python改进猜数字的游戏
当用户输入错误类型的时候,及时提醒用户重新输入,防止程序崩溃。
import random
print ('从0到10之间,请输入一个数字:')
answer = input()
guess = int(random.randint(0,10))
times = 3
if answer not answer.isdigit():
print (False, '输入错误,请再次输入一个数字从0到10之间')
answer = input()
while answer == answer.isdigit():
answer = int(input())
print (True)
while (answer != guess) and (times > 0):
times = times - 1
print ('猜错了,重新再猜一次吧:')
second_chance = int(input())
if answer == guess:
print ('恭喜你猜对了!')
if second_chance > guess:
print ('大了')
else:
print ('小了')
print ('电脑心理想的是:', guess, '游戏结束,不玩啦~')
思路:如果answer 不是answer.isdigit()那么就在输入一次,while (answer != guess) and (times >0)这条开始就没有问题了。
请问下为什么输入数字了之后代码就不跑了?
还有if answer not answer.isdigit()为什么有语法错误啊?