我正在尝试编写一个程序,该程序将打折后打印总价。如果价格paid
超过100
,它将关闭20%
。在程序结束时,我收到此错误:
if paid>=100:
TypeError: unorderable types: str() >= int()
这是我的代码:
paid=input('How much did you pay? ')
if paid>=100:
actualPay=100*1.25 #20% off total, 100/0.80 = 1.25
print(actualPay)
最佳答案
将input
强制转换为int
paid=int(input('How much did you pay? '))
input
始终返回一个字符串。您需要显式转换它。例如:
paid=int(input('How much did you pay? '))
if paid>=100:
actualPay=100*1.25
print(actualPay)
更好的方法是使用
try
和except
块。try:
paid=int(input('How much did you pay? '))
if paid>=100:
actualPay=100*1.25
print(actualPay)
except TypeError:
print "Please enter a valid input."
关于python - Python-转换错误,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/28862082/