while 循环报错,求大神鞭策
temp = input('选一个数字进行输入')guess = int(temp)
whileguess != 8:
if guess < 8:
temp = input('小了,请重新输入:')
guess = int(temp)
elif guess > 8:
guess = input('大了,请重新输入:')
int(guess)
else:
print('你终于答对了')
print('终于答对了')运行之后就是下面这样,三次之后直接报错是什么情况
==
选一个数字进行输入7
小了,请重新输入:9
大了,请重新输入:7
Traceback (most recent call last):
File "D:\python3\第二讲改进3.py", line 4, in <module>
if guess < 8:
TypeError: '<' not supported between instances of 'str' and 'int'
>>>
改成这样,最后的print语句需要在while循坏外,要不然答对了也没任何响应
temp = input('选一个数字进行输入')
guess = int(temp)
whileguess != 8:
if guess < 8:
temp = input('小了,请重新输入:')
guess = int(temp)
elif guess > 8:
guess = input('大了,请重新输入:')
guess = int(guess)
print('你终于答对了')
页:
[1]