请问“无法排序的类型:int()<str()”怎么办

我正在尝试在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))

它告诉我:


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()

有什么想法可以解决这个问题吗?


噜噜哒
浏览 394回答 2
2回答

海绵宝宝撒

附带说明一下,在Python 2.0中,您可以将任何事物与任何事物进行比较(从int到string)。由于这不是很明显,因此在3.0中进行了更改,这是一件好事,因为您不会遇到将无意义的值相互比较的麻烦,或者当您忘记转换类型时。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python