猜猜python中的数字。结束游戏的选项
我是Python和编程新手,我正在尝试制作“猜数字”游戏。我让它工作,但我想添加选项,以关闭游戏使用刹车声明。我已经试了两天了,但我想我卡住了 以下是有效的代码(没有关闭游戏的选项):猜猜python中的数字。结束游戏的选项,python,python-3.x,Python,Python 3.x,我是Python和编程新手,我正在尝试制作“猜数字”游戏。我让它工作,但我想添加选项,以关闭游戏使用刹车声明。我已经试了两天了,但我想我卡住了 以下是有效的代码(没有关闭游戏的选项): 随机导入 尝试次数=0 secretNumber=random.randint(1100) 尽管如此: 打印(“猜测1到100之间的数字”) 猜测=输入() 猜测=int(猜测) 尝试次数=尝试次数+1 如果猜测>100或猜测0: 打印(“不!请尝试更高的数字!\n”) elif guess>secretNumb
随机导入
尝试次数=0
secretNumber=random.randint(1100)
尽管如此:
打印(“猜测1到100之间的数字”)
猜测=输入()
猜测=int(猜测)
尝试次数=尝试次数+1
如果猜测>100或猜测0:
打印(“不!请尝试更高的数字!\n”)
elif guess>secretNumber和guess 100或guess 0:
打印(“不!请尝试更高的数字!\n”)
elif guess>secretNumber和guess变化不大,一切正常
import random
attempts = 0
secretNumber = random.randint(1,100)
while True:
print("Guess a number between 1 and 100. You can exit game anytime by typing q ")
guess=input()
stop=str(guess)
if stop== 'q':
print("You quitted the game")
break
elif not guess.isdigit():
print("Only numbers are allowed")
else:
guess = int(guess)
attempts = attempts + 1
if guess >100 or guess<=0:
attempts = attempts - 1
print("Not allowed.Only numbers between 1 and 100 allowed! \n")
elif guess < secretNumber and guess >0:
print("Nope!Try a higher number! \n")
elif guess > secretNumber and guess <100:
print("Nope!Try a lower number \n")
if guess == secretNumber:
attempts = str(attempts)
print("Congratulations you found it after " + attempts + " attempts")
break
随机导入
尝试次数=0
secretNumber=random.randint(1100)
尽管如此:
打印(“猜一个介于1和100之间的数字。您可以通过键入q随时退出游戏”)
猜测=输入()
停止=str(猜测)
如果停止=='q':
打印(“你退出了游戏”)
打破
elif not guess.isdigit():
打印(“只允许数字”)
其他:
猜测=int(猜测)
尝试次数=尝试次数+1
如果猜测>100或猜测0:
打印(“不!请尝试更高的数字!\n”)
elif guess>secretNumber和guess在Nick A和timgeb在评论中提到的内容下面,您可以修改一些内容。
正如我在下面的脚本中所建议的那样,您可能想看看。请在脚本中查找注释
import random
attempts = 0
secretNumber = random.randint(1,100)
# you can prompt the user within input()
stop=input("Guess a number between 1 and 100. You can exit game anytime by typing quit ")
while True:
if stop == "quit": # as the the output of input() is a string, you can compare it with a string without str()
print("You quitted the game")
break
try: # work with try and except to avoid problems whenever the user writes something else than 'quit' or an integer
guess = int(stop)
attempts += 1
if guess >100 or guess<=0:
attempts = attempts - 1
print("Not allowed.Only numbers between 1 and 100 allowed! \n")
elif guess < secretNumber and guess >0:
print("Nope!Try a higher number! \n")
elif guess > secretNumber and guess <=100: # note that it's <=. Otherwise nothing happens when the users chose 100
print("Nope!Try a lower number \n")
except: # if it's not possible to turn 'stop' into an integer we'd like to perform the following
stop = input("No valid input, please guess again ") # Don't stop the script, let the user guess again
continue # don't stop, but leave the current iteration (don't execute the rest) and start again at the beginning of the loop
if guess == secretNumber:
attempts = str(attempts)
print("Congratulations you found it after " + attempts + " attempts")
break
stop = input("Guess again ") # prompt for a new number every round of the loop
随机导入
尝试次数=0
secretNumber=random.randint(1100)
#您可以在input()中提示用户
停止=输入(“猜一个介于1和100之间的数字。您可以通过键入quit随时退出游戏”)
尽管如此:
如果stop==“quit”:#由于input()的输出是一个字符串,因此可以将其与不带str()的字符串进行比较
打印(“你退出了游戏”)
打破
try:#使用try和except,以避免在用户写入“quit”或整数以外的内容时出现问题
猜测=int(停止)
尝试次数+=1
如果猜测>100或猜测0:
打印(“不!请尝试更高的数字!\n”)
elif guess>secretNumber和guess删除您的否则:继续<代码>继续
使代码在循环开始时再次继续。并且在中断之前打印
。@Nick A和timgeb感谢您的回答。我两个都做了,但仍然不起作用。输入一个数字后,什么也没有发生,我必须再次键入该数字才能工作,如果我不这样做,只需再次按enter键,我会得到一个ValueError,当我键入quit时,它会工作,但我会得到一个NameError..非常感谢@legendisback!!绝对是一个传奇……它成功了!我一直在尝试使用.isdigit()方法,以避免在用户输入str而不是int时出现ValueError,我尝试将“q”字更改为0,以避免在没有运气的情况下发生冲突。这可能吗?有什么想法吗?非常感谢您的时间。@Mike\u R现在编辑了这个程序。添加了isdigit()只允许数字猜测!再次非常感谢!在Python3中,input
中的值已经是一个字符串,无需使用str()
进行转换,它工作得很好,但我不希望当用户提交无效值时程序停止,因此我删除了“except:”后面的“break”,循环不会停止。当第一个提交的值无效时就会出现问题..我得到一个NameError。有没有想过我该如何解决这个问题?再次感谢您抽出时间!如果您删除了break
,并且用户提交了一个无效输入,则不会定义guess
,因为无法将无效输入转换为整数。基本上,您可以更改相应的if
语句,并使用continue
而不是break
。我对脚本进行了更新,并对其工作原理进行了评论。您可以了解有关continue语句的更多信息。请随时提出任何进一步的问题!
import random
attempts = 0
secretNumber = random.randint(1,100)
while True:
print("Guess a number between 1 and 100. You can exit game anytime by typing q ")
guess=input()
stop=str(guess)
if stop== 'q':
print("You quitted the game")
break
elif not guess.isdigit():
print("Only numbers are allowed")
else:
guess = int(guess)
attempts = attempts + 1
if guess >100 or guess<=0:
attempts = attempts - 1
print("Not allowed.Only numbers between 1 and 100 allowed! \n")
elif guess < secretNumber and guess >0:
print("Nope!Try a higher number! \n")
elif guess > secretNumber and guess <100:
print("Nope!Try a lower number \n")
if guess == secretNumber:
attempts = str(attempts)
print("Congratulations you found it after " + attempts + " attempts")
break
import random
attempts = 0
secretNumber = random.randint(1,100)
# you can prompt the user within input()
stop=input("Guess a number between 1 and 100. You can exit game anytime by typing quit ")
while True:
if stop == "quit": # as the the output of input() is a string, you can compare it with a string without str()
print("You quitted the game")
break
try: # work with try and except to avoid problems whenever the user writes something else than 'quit' or an integer
guess = int(stop)
attempts += 1
if guess >100 or guess<=0:
attempts = attempts - 1
print("Not allowed.Only numbers between 1 and 100 allowed! \n")
elif guess < secretNumber and guess >0:
print("Nope!Try a higher number! \n")
elif guess > secretNumber and guess <=100: # note that it's <=. Otherwise nothing happens when the users chose 100
print("Nope!Try a lower number \n")
except: # if it's not possible to turn 'stop' into an integer we'd like to perform the following
stop = input("No valid input, please guess again ") # Don't stop the script, let the user guess again
continue # don't stop, but leave the current iteration (don't execute the rest) and start again at the beginning of the loop
if guess == secretNumber:
attempts = str(attempts)
print("Congratulations you found it after " + attempts + " attempts")
break
stop = input("Guess again ") # prompt for a new number every round of the loop