你的思路是错的,guess = int(input()) 这一句已经将 guess 转化为 int() 了,如果用户不输入整数会直接报错。
而且判断一个字符串可不可以强制转化为整数,需要使用字符串的 isdigit() 方法。
代码帮你改了:
import random
times = 3
secret = random.randint(1, 10)
print('Welcome!!!!!!')
guess = 0
print("Guess what number do I think?", end=" ")
while (guess != secret) and (times > 0):
guess = input()
while not guess.isdigit():
guess = input("Illegal type! Please input: ")
times = times - 1
if guess == secret:
print("Bingo!")
print("Huh, without prize!")
else:
if guess > secret:
print("Too big!")
else:
print("Too small!")
if times > 0:
print("Try again!")
else:
print("You are out of chance!")
print("Game over!")
|