程序本应转到一个函数,但却遵循了 try 和 except 循环
Program meant to go to a function but instead follows try and except loop
我决定在Python 3.3 中制作一个基础游戏,怪物和宝物分别生成在1-100 的数字上,用户必须选择一个数字。如果他们离得太近或太高,或者他们是否撞到什么东西,程序就会通知用户。
我做了一个 try 循环,除了 NameError(因为我在 Mac,它是 Windows 的 ValueError)。在我提出任何问题之前,我想与您分享我目前拥有的代码:
import time, random
def generateValues():
global treasurePos, monsterPos
while treasurePos != monsterPos:
treasurePos = random.randint(1, 100)
monsterPos = random.randint(1, 100)
print(treasurePos)
print(monsterPos)
def mainmenu():
time.sleep(0.5)
print("welcome to this game, press 1 to play or 2 to quit")
time.sleep(0.5)
try:
option = int(input("What would you like to do: "))
if option == (1):
time.sleep(0.5)
generateValues()
elif option == (2):
quit()
except NameError:
time.sleep(0.5)
print("please enter only 1 or 2")
mainmenu()
如果我输入1,我的原计划是让游戏继续并生成宝物和怪物的位置。相反,程序所做的是循环回到这个:
except NameError:
time.sleep(0.5)
print("please enter only 1 or 2")
这进一步创建了 'please enter only 1 or 2' 的无限循环 - 即使我确实输入了 1.
因此,我的问题是,是否可以执行简单或复杂的命令来阻止此循环继续并允许程序继续执行 'generateValues' 函数?
如果您愿意分享任何帮助或见解,我将不胜感激。
提前致谢,
狮子座
编辑 1:
Karen Clark 指出了我的 while 循环,我在下面为其编写了一个替代解决方案,该解决方案有效:
def generateValues():
global treasurePos, monsterPos
treasurePos = 0
monsterPos = 0
treasurePos = random.randint(1, 3)
monsterPos = random.randint(1, 5)
if treasurePos == monsterPos:
generateValues()
else:
print(treasurePos)
print(monsterPos)
问题出在 generateValues() 函数中。您需要初始化 treasurePos 和 monsterPos 才能工作。试试这个:
def generateValues():
global treasurePos, monsterPos
treasurePos = 10
monsterPos = 1
while treasurePos != monsterPos:
treasurePos = random.randint(1, 100)
monsterPos = random.randint(1, 100)
print(treasurePos)
print(monsterPos)
只要确保给它们不同的值即可。 If, not 代码不会进入循环。
您在调用 generateCharPosition() 时输入错误。函数定义正确,但调用缺少 "i" - generateCharPosition
我决定在Python 3.3 中制作一个基础游戏,怪物和宝物分别生成在1-100 的数字上,用户必须选择一个数字。如果他们离得太近或太高,或者他们是否撞到什么东西,程序就会通知用户。
我做了一个 try 循环,除了 NameError(因为我在 Mac,它是 Windows 的 ValueError)。在我提出任何问题之前,我想与您分享我目前拥有的代码:
import time, random
def generateValues():
global treasurePos, monsterPos
while treasurePos != monsterPos:
treasurePos = random.randint(1, 100)
monsterPos = random.randint(1, 100)
print(treasurePos)
print(monsterPos)
def mainmenu():
time.sleep(0.5)
print("welcome to this game, press 1 to play or 2 to quit")
time.sleep(0.5)
try:
option = int(input("What would you like to do: "))
if option == (1):
time.sleep(0.5)
generateValues()
elif option == (2):
quit()
except NameError:
time.sleep(0.5)
print("please enter only 1 or 2")
mainmenu()
如果我输入1,我的原计划是让游戏继续并生成宝物和怪物的位置。相反,程序所做的是循环回到这个:
except NameError:
time.sleep(0.5)
print("please enter only 1 or 2")
这进一步创建了 'please enter only 1 or 2' 的无限循环 - 即使我确实输入了 1.
因此,我的问题是,是否可以执行简单或复杂的命令来阻止此循环继续并允许程序继续执行 'generateValues' 函数?
如果您愿意分享任何帮助或见解,我将不胜感激。
提前致谢, 狮子座
编辑 1:
Karen Clark 指出了我的 while 循环,我在下面为其编写了一个替代解决方案,该解决方案有效:
def generateValues():
global treasurePos, monsterPos
treasurePos = 0
monsterPos = 0
treasurePos = random.randint(1, 3)
monsterPos = random.randint(1, 5)
if treasurePos == monsterPos:
generateValues()
else:
print(treasurePos)
print(monsterPos)
问题出在 generateValues() 函数中。您需要初始化 treasurePos 和 monsterPos 才能工作。试试这个:
def generateValues():
global treasurePos, monsterPos
treasurePos = 10
monsterPos = 1
while treasurePos != monsterPos:
treasurePos = random.randint(1, 100)
monsterPos = random.randint(1, 100)
print(treasurePos)
print(monsterPos)
只要确保给它们不同的值即可。 If, not 代码不会进入循环。
您在调用 generateCharPosition() 时输入错误。函数定义正确,但调用缺少 "i" - generateCharPosition