Python “不可排序的类型:int() < str()”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14886881/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
"Unorderable types: int() < str()"
提问by user2074050
I'm trying to make a retirement calculator right now on Python. There's nothing wrong with the syntax but when I run the following program:
我现在正在尝试在 Python 上制作一个退休计算器。语法没有任何问题,但是当我运行以下程序时:
def main():
print("Let me Retire Financial Calculator")
deposit = input("Please input annual deposit in dollars: $")
rate = input ("Please input annual rate in percentage: %")
time = input("How many years until retirement?")
x = 0
value = 0
while (x < time):
x = x + 1
value = (value * rate) + deposit
print("The value of your account after" +str(time) + "years will be $" + str(value))
It tells me that:
它告诉我:
Traceback (most recent call last):
File "/Users/myname/Documents/Let Me Retire.py", line 8, in <module>
while (x < time):
TypeError: unorderable types: int() < str()
Any ideas how I could solve this?
我有什么想法可以解决这个问题吗?
采纳答案by Gareth Latty
The issue here is that input()returns a string in Python 3.x, so when you do your comparison, you are comparing a string and an integer, which isn't well defined (what if the string is a word, how does one compare a string and a number?) - in this case Python doesn't guess, it throws an error.
这里的问题是input()在 Python 3.x中返回一个字符串,所以当你进行比较时,你是在比较一个字符串和一个没有明确定义的整数(如果字符串是一个单词,如何比较一个字符串和数字?) - 在这种情况下,Python 不会猜测,它会引发错误。
To fix this, simply call int()to convert your string to an integer:
要解决此问题,只需调用int()将字符串转换为整数:
int(input(...))
As a note, if you want to deal with decimal numbers, you will want to use one of float()or decimal.Decimal()(depending on your accuracy and speed needs).
请注意,如果您想处理十进制数,您将需要使用float()or 之一decimal.Decimal()(取决于您的准确性和速度需求)。
Note that the more pythonic way of looping over a series of numbers (as opposed to a whileloop and counting) is to use range(). For example:
请注意,循环一系列数字(与while循环和计数相反)的更pythonic 方法是使用range(). 例如:
def main():
print("Let me Retire Financial Calculator")
deposit = float(input("Please input annual deposit in dollars: $"))
rate = int(input ("Please input annual rate in percentage: %")) / 100
time = int(input("How many years until retirement?"))
value = 0
for x in range(1, time+1):
value = (value * rate) + deposit
print("The value of your account after" + str(x) + "years will be $" + str(value))
回答by user1767754
Just a side note, in Python 2.0 you could compare anything to anything (int to string). As this wasn't explicit, it was changed in 3.0, which is a good thing as you are not running into the trouble of comparing senseless values with each other or when you forget to convert a type.
顺便提一下,在 Python 2.0 中,您可以将任何内容与任何内容进行比较(int 到 string)。由于这不是明确的,它在 3.0 中进行了更改,这是一件好事,因为您不会遇到相互比较无意义值或忘记转换类型的麻烦。

