我正在做一个测验来帮助我修改天文学,但是即使我正确输入了代码,这个错误也出现了:
What is the diameter of the Earth? 13000
Traceback (most recent call last):
File "C:/Users/reddo/Documents/Python stuff/Astronomy quiz.py", line 19, in <module>
elif answer1 >= 13001:
TypeError: '>=' not supported between instances of 'str' and 'int'
>>>
以下是我编写代码的方式:
answer1 = 13000
answer1 = input ("What is the diameter of the Earth? ")
if answer1 == 13000:
print("Correct!")
elif answer1 >= 13001:
print ("Incorrect!")
elif answer1 <= 12999:
print ("Incorrect!")
我不知道我是否出错了,或者只是程序本身。
答案 0 :(得分:2)
您应该将其从字符串转换为整数
answer1 = int(input ("What is the diameter of the Earth? "))
答案 1 :(得分:1)
answer1是一个字符串,因此您无法将其与整数进行比较。用以下内容替换第二行,将其转换为整数:
answer1 = int(input("What is the diameter of the Earth? "))