Why do I get Unorderable types: int() str() or '' not supported between instances of 'int' and 'str'?
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))
Python - TypeError: unorderable types: str() int()
Python cannot order str
values and int
values together.
In your example 99999
is an int
value while all the other values are str
values.
TypeError: unorderable types: str() = int()
The reason to this is that the function check_pb_ratio.get_price_book()
returns a string, not an int. Python doesn't want to know the similarity between '1' and 1.
So, the way to fix it is: add int()
or float()
around check_pb_ratio.get_price_book()
TypeError: unorderable types: int() str()
In python3, input
returns a string. You pass this string to your function directly and compare with integers obtained from random.choice
. This is what happens:
In [535]: '1' > 2
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-535-6857a5c3feea> in <module>()
----> 1 '1' > 2
TypeError: unorderable types: str() > int()
Conclusion: int
s and str
ings are not comparable. You'll need to convert the input by calling the int
function.
your_guess = int(input(...))
And then, it should work:
In [536]: 1 > 2
Out[536]: False
However, this solves only one problem. Every time you call your function, number
is re-initialised with a new value each time, so that if you guess against a number and call the function again, you will be guessing against a totally different random number next time. You'll want to set numbers
outside the function and then call the function, passing the guess and random number as arguments.
Why am I getting TypeError: unorderable types: str() int() in this code?
Sorry, I figured this out. For some reason, during the first run it saved the JSON key as a string. In the second run, it saved it as an integer. self.changes
was printing as:
{'2': {'date': '2018-06-29 02:02:03', 'change': 'TEST'}, '1': {'date': '2018-06-29 01:07:37', 'change': 'TEST'}}
To fix that, I just changed
self.changes[count] = {'date': now.strftime("%Y-%m-%d %H:%M:%S"), 'change': change}
to
self.changes[str(count)] = {'date': now.strftime("%Y-%m-%d %H:%M:%S"), 'change': change}
Related Topics
Standard Way to Embed Version into Python Package
Rename Specific Column(S) in Pandas
What Version of Visual Studio Is Python on My Computer Compiled With
Shuffle an Array with Python, Randomize Array Item Order with Python
What Do I Use for a Max-Heap Implementation in Python
How to Specify an Authenticated Proxy for a Python Http Connection
Replacing Text in a File with Python
How to Get Last Items of a List in Python
Heapq with Custom Compare Predicate
Extract Subset of Key-Value Pairs from Dictionary
Add Column to Dataframe with Constant Value
Pandas: Looking Up the List of Sheets in an Excel File
How to Open a File for Exclusive Access in Python
How to Use Brew Installed Python as the Default Python
Python Numpy/Scipy Curve Fitting
The Zip() Function in Python 3
Meaning of Inter_Op_Parallelism_Threads and Intra_Op_Parallelism_Threads