import random
n = 1
tala = random.randrange(0,11)
print('talan er ' + str(tala))
print('The goal is to guess the number.')
num = input('Pick a number: ')
while num != tala:
if num < tala:
print('Too Low')
num = input('Try again: ')
elif num > tala:
print('Too High')
num = input('Try again: ')
else:
print('wow congratulations, you guessed the right number in ' + str(n) + ' tries.')
n += 1
上面是我的代码,下面是我遇到的错误。
我似乎找不到我的错误:这只是一个简单的代码,但是我找不到该错误。
Traceback (most recent call last):
File "C:/Users/ebben/PycharmProjects/HelloWorld/GuessNumber.py", line 10, in <module>
if num < tala:
TypeError: '<' not supported between instances of 'str' and 'int'
答案 0 :(得分:2)
python输入始终是字符串,您需要通过添加int()
方法
这是您的代码:
您需要更改num
:
import random
n = 1
tala = random.randrange(0,11)
print('talan er ' + str(tala))
print('The goal is to guess the number.')
num = int(input('Pick a number: '))
while num != tala:
if num < tala:
print('Too Low')
num = int(input('Try again: '))
elif num > tala:
print('Too High')
num = int(input('Try again: '))
else:
print('wow congratulations, you guessed the right number in ' + str(n) + ' tries.')
n += 1
答案 1 :(得分:0)
错误表明,少于<
运算符和>
运算符必须与number
或int
的{{1}}数据类型进行比较。这意味着变量float
是num
对象。
因此,替换以下行以解决您的错误:-
替换
string
进入
if num < tala:
替换
if int(num) < tala:
进入
if num > tala: