Python 3.x 程序无故退出python
所以我打了这个,我已经看了几个小时了,我似乎不明白它到底出了什么问题。当我运行它并选择一个选项时,它就退出了。我使用IDLE来运行它,为了运行函数dice()或guess(),我必须手动执行。提前谢谢Python 3.x 程序无故退出python,python-3.x,Python 3.x,所以我打了这个,我已经看了几个小时了,我似乎不明白它到底出了什么问题。当我运行它并选择一个选项时,它就退出了。我使用IDLE来运行它,为了运行函数dice()或guess(),我必须手动执行。提前谢谢 from random import randint def main (): print ("Choose a game.") print ("1. Dice") print ("2. Guess") choice = input ("Enter your ch
from random import randint
def main ():
print ("Choose a game.")
print ("1. Dice")
print ("2. Guess")
choice = input ("Enter your choice: ")
if choice == 1:
dice ()
elif choice == 2:
guess ()
elif choice == 'Q' or choice == 'q':
exit ()
def dice ():
rolling = True
while rolling:
n = input ("How many dice? ")
if n == 'M' or n == 'm':
main ()
elif n == 'Q' or n == 'q':
exit ()
else:
for i in range (int (n)):
print (randint (1, 6))
def guess ():
guessing = True
while guessing:
print ("Guess the number: 1-1000")
n = randint (1, 1000)
count = 0
wrong = True
while wrong:
g = input ("Guess: ")
if g == 'M' or g == 'm':
main ()
elif g == 'Q' or g == 'q':
exit ()
else:
if int (g) > n:
print ("Lower")
count += 1
elif int (g) < n:
print ("Higher")
count += 1
else:
print ("Correct")
count += 1
wrong = False
print ("You took " + str (count) + " guesses")
main ()
来自随机导入randint
defmain():
打印(“选择游戏”)
打印(“1.骰子”)
打印(“2.猜测”)
选择=输入(“输入您的选择:”)
如果选项==1:
骰子()
elif选项==2:
猜()
elif choice=='Q'或choice=='Q':
退出()
def dice():
滚动=真
滚动时:
n=输入(“多少个骰子?”)
如果n='M'或n='M':
主要()
elif n=='Q'或n=='Q':
退出()
其他:
对于范围内的i(int(n)):
印刷品(randint(1,6))
def guess():
猜测=正确
猜测时:
打印(“猜测数字:1-1000”)
n=randint(11000)
计数=0
错=真
虽然错误:
g=输入(“猜测:”)
如果g='M'或g='M':
主要()
elif g=='Q'或g=='Q':
退出()
其他:
如果int(g)>n:
打印(“下”)
计数+=1
elif int(g)
正如注释中指出的,问题的原因是input
返回字符串,但您正在检查与整数的等价性。以下代码应该可以工作:
def main ():
print ("Choose a game.")
print ("1. Dice")
print ("2. Guess")
choice = input ("Enter your choice: ")
if choice == '1':
dice ()
elif choice == '2':
guess ()
elif choice == 'Q' or choice == 'q':
exit ()
我修复了我在评论中指出的比较错误。优化了程序流程,因此您不会像另一个中所指出的那样深入到函数调用堆栈中:
from random import choices, randint
def quit():
exit(0)
def myInput(text):
"""Uses input() with text, returns an int if possible, uppercase trimmed string else."""
p = input(text).strip().upper()
try:
p = int(p)
except ValueError:
pass
return p
def dice ():
while True:
n = myInput ("How many dice? ")
if n == 'M':
break # back to main() function
elif n == 'Q':
quit() # quit the game
elif isinstance(n,int): # got an int, can work with that
# get all ints in one call, print decomposed list with sep of newline
print (*choices(range(1,7), k = n), sep="\n")
else: # got no int, so error input
print("Input incorrect: Q,M or a number accepted.")
def guess ():
while True:
print ("Guess the number: 1-1000")
n = randint (1, 1000)
count = 0
while True:
g = myInput ("Guess: ")
if g == 'M':
return # back to main menue
elif g == 'Q':
quit () # quit the game
elif isinstance(g,int): # got a number, can work with it
count += 1
if g != n: # coditional output, either lower or higher
print ("Lower" if g < n else "Higher")
else:
print ("Correct")
print ("You took {} guesses".format(count))
else: # not a number, incorrect
print("Input incorrect: Q,M or a number accepted.")
# which function to call on which input
options = { "1" : dice, "2" : guess, "Q":quit }
def main ():
while True:
print ("Choose a game.")
print ("1. Dice")
print ("2. Guess")
choice = input ("Enter your choice: ").upper()
if choice in options:
options[choice]() # call the function from the options-dict
else:
print("Wrong input.")
main ()
来自随机导入选项,randint
def quit():
出口(0)
def myInput(文本):
“”“将input()与文本一起使用,如果可能,返回一个int,否则返回大写字符串。”“”
p=输入(文本).strip().upper()
尝试:
p=int(p)
除值错误外:
通过
返回p
def dice():
尽管如此:
n=myInput(“多少个骰子?”)
如果n==“M”:
中断#返回main()函数
elif n=='Q':
退出游戏
elif isinstance(n,int):#得到一个int,可以使用它
#在一次调用中获取所有整数,使用换行符的sep打印分解列表
打印(*选项(范围(1,7),k=n),sep=“\n”)
else:#没有int,因此输入错误
打印(“输入错误:Q、M或接受的数字”)
def guess():
尽管如此:
打印(“猜测数字:1-1000”)
n=randint(11000)
计数=0
尽管如此:
g=myInput(“猜测:”)
如果g=='M':
返回#返回主菜单
elif g=='Q':
退出游戏
elif isinstance(g,int):#有一个数字,可以使用它
计数+=1
如果g!=n:#编码输出,低或高
打印(“如果g
input中的输入总是一个字符串。如果希望选择是int
,请尝试If int(choice)==1
。作为补充,如果您的elif
s下有一个打印Wow的else
,您可能会省去一些麻烦,我没想到会有这个选项,所以您知道在哪里可以找到。作为另一个附录,不要在函数名和括号之间留空格exit()。无需对其调用int()
,如果有人输入非数字,如“Q”
@MoxieBall,则它只会抛出valueerrors,因为“Q”
是有效输入,使用int()
将导致valueerrors。比较'1'
&&
'2'
字符串。如果你只做choice=input(“输入你的选择”)。upper()
那么你的输入是upper()ed,一旦你可以省去所有与lower()ed字符的二次比较,就可以了!我完全忘记了。