Why do I get "Unorderable types: int() < str()" or "'<' not supported between instances of 'int' and 'str'" when comparing input to a number? [duplicate]
Asked Answered
W

2

16

I have this code:

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

I get this error message:

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

How can I fix it?

Whoops answered 15/2, 2013 at 1:8 Comment(0)
G
37

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.

To fix this, simply call int() to convert your string to an integer:

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

Note that the more pythonic way of looping over a series of numbers (as opposed to a while loop and counting) is to use range(). For example:

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))
Greenwell answered 15/2, 2013 at 1:9 Comment(2)
okay I figured it all out. Thank you so very much for your time and effort. I really do appreciate it. Thank you so much kind sir. There's one last issue to resolve which is that the annual rate decreases with time. For example if I input 500 dollars over 10 years at 50% rate it gives me 550 dollars after one year, 555.0, 555.55, 555.5555, etc...As in it doesn't actually do 50 percent yearly.Whoops
@Whoops That's just a maths error. You are adding to the deposit, not the current value. You want value *= (1 + rate) (multiply last year's value by the rate plus one).Greenwell
K
1

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.

Kyser answered 26/6, 2017 at 21:29 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.