因为标题说我有2个问题(我知道)第一个当我输入退出它作为数字我将如何使用数字验证它并打印错误消息第二个如果我猜一个数字让我们说20它会说太低然后如果我把它说得太高了
这是命令提示符
输入1到100之间的数字或输入exit退出:20
太低了,再试一次
输入1到100之间的数字或输入exit退出:21
太高了,再试一次
You have already retweeted this tweet.
答案 0 :(得分:0)
您可以查看isdigit
:
#!/usr/bin/env python
import random
guess = '0'
while (guess) != exit:
answer = random.randrange(1,100)
answer = str(answer)
guess = input('Enter a number between 1 and 100 or enter exit to exit: ')
if not guess.isdigit():
if guess == "exit":
break
guess = (guess)
if guess == answer:
print('Correct')
elif guess > answer :
print('Too high, try again')
elif guess < answer :
print('Too low, try again')