Python 我怎么能在一个输入中做一个有限的数字,并且只做数字,我不想要符号
我有一个小游戏,它让你选择一个随机的游戏,但我有两个问题: 1-我想验证用户输入是否为介于0和20之间的数字 2-我想让用户再次播放(可以通过询问他或直接播放) 这是我的代码,如果有任何问题或建议,请告诉我,我希望你能写在一封信中,这样我就可以理解它,非常感谢你的帮助 代码如下:Python 我怎么能在一个输入中做一个有限的数字,并且只做数字,我不想要符号,python,python-3.x,input,while-loop,Python,Python 3.x,Input,While Loop,我有一个小游戏,它让你选择一个随机的游戏,但我有两个问题: 1-我想验证用户输入是否为介于0和20之间的数字 2-我想让用户再次播放(可以通过询问他或直接播放) 这是我的代码,如果有任何问题或建议,请告诉我,我希望你能写在一封信中,这样我就可以理解它,非常感谢你的帮助 代码如下: import random rn = int(random.randint(0, 20)) gn = int(input("write a number from 0-20: ")) while True: i
import random
rn = int(random.randint(0, 20))
gn = int(input("write a number from 0-20: "))
while True:
if rn < gn:
print("You were too close, try again")
break
elif rn == gn:
print("Oh, I lost, you win, good game (GG)")
break
if rn > gn:
print("You are not close, try again")
break
while False:
rn = int(random.randint(0, 20))
gn = int(input("write a number from 0-20: "))
随机导入
rn=int(random.randint(0,20))
gn=int(输入(“写一个0-20之间的数字:”)
尽管如此:
如果rngn:
打印(“您尚未关闭,请重试”)
打破
虽然错误:
rn=int(random.randint(0,20))
gn=int(输入(“写一个0-20之间的数字:”)
我将代码尽可能模块化,并且非常简单
def checkInput(inp):
try:
n = int(inp)
except:
return (False,"Please enter an integer between 0 to 20 and nothing else")
if n >=0 and n<=20:
return (True,n)
else:
return (False, "Your number is not in the expected range of 0 to 20")
def gameInput():
import random
rn = int(random.randint(0, 20))
gn = input("write a number from 0-20: ")
while(not checkInput(gn)[0]):
print(checkInput(gn)[1])
gn = input("write a number from 0-20: ")
gn = checkInput(gn)[1]
return gn,rn
def gameCheck():
gn,rn = gameInput()
# print(gn,rn)
if rn < gn:
print("You were too close, try again")
elif rn == gn:
print("Oh, I lost, you win, good game (GG)")
elif rn > gn:
print("You are not close, try again")
def game():
gameCheck()
playAgain = input("Do you want to play again? Enter Y for yes and anything else for no")
while playAgain.lower() =="y":
gameCheck()
playAgain = input("Do you want to play again? Enter Y for yes and anything else for no")
game()
def检查输入(inp):
尝试:
n=int(inp)
除:
return(False,“请输入一个介于0到20之间的整数,不要输入其他值”)
如果n>=0且n gn:
打印(“您尚未关闭,请重试”)
def game():
gameCheck()
再次播放=输入(“您想再次播放吗?输入Y表示是,输入其他任何内容表示否”)
再次播放时。lower()=“y”:
gameCheck()
再次播放=输入(“您想再次播放吗?输入Y表示是,输入其他任何内容表示否”)
游戏()
要检查输入值,请尝试以下操作:
def ask_for_guess():
# the function will ask infinitely for input number unless it's valid number in between 0-20
while True:
guess_input = input("write a number from 0-20: ")
try:
gn = int(guess_input)
assert 0 <= gn <= 20, "Number must be between 0-20"
return gn
except ValueError as ex:
# ValueError will be thrown if the conversion to int will fail
print(f"{guess_input} is not a number, please try again")
except AssertionError as ex:
# AssertionError will be thrown by the test in the try block if the number is not in range
print(str(ex))
def ask_for_guess():
#函数将无限次地请求输入数字,除非它是介于0-20之间的有效数字
尽管如此:
猜一猜输入=输入(“写一个0-20之间的数字:”)
尝试:
gn=int(猜测输入)
断言0您将首先重新阅读if和while语句的工作方式。你的循环是非感官的。
play_again = True
while play_again:
rn = int(random.randint(0, 20))
gn = ask_for_guess()
if rn < gn:
print("You were too close, try again")
elif rn == gn:
print("Oh, I lost, you win, good game (GG)")
else:
print("You are not close, try again")
# if the user doesn't want to play again, the play_again variable will be False and the code won't enter for another iteration inside the while loop
play_again = input("Do you want to play again? [y/n]") == "y"