TypeError: '>=' not supported between instances of 'str' and 'int' - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: TypeError: '>=' not supported between instances of 'str' and 'int' (/thread-20466.html) |
TypeError: '>=' not supported between instances of 'str' and 'int' - AsadZ - Aug-12-2019 num0 = input("Enter a Number: ") if not(num0>=0): print("Please enter a Valid Number >=0.") else: print(num0)I need help, when i run run this code i get a error>>>>>> Traceback (most recent call last): File "C:/Users/Human/PycharmProject/First/12.py", line 2, in <module> if not(num0>=0): TypeError: '>=' not supported between instances of 'str' and 'int' Process finished with exit code RE: TypeError: '>=' not supported between instances of 'str' and 'int' - Axel_Erfurt - Aug-12-2019 if not int(num0) >= 0: RE: TypeError: '>=' not supported between instances of 'str' and 'int' - ThomasL - Aug-12-2019 input() returns what you typed in as a string so this must be converted into a number using the int() function. Of course if you type ABC the function int() throws an error as this cannot be converted into a number. RE: TypeError: '>=' not supported between instances of 'str' and 'int' - ndc85430 - Aug-12-2019 (Aug-12-2019, 07:27 PM)ThomasL Wrote: Of course if you type ABC the function int() throws an error as this cannot be converted into a number. It's a valid number in hexadecimal, so int wouldn't throw if you passed base=16 .
RE: TypeError: '>=' not supported between instances of 'str' and 'int' - AsadZ - Aug-15-2019 (Aug-12-2019, 07:30 PM)ndc85430 Wrote:@ThomasL is there any solution for this. i am trying to build a basic calculator(Aug-12-2019, 07:27 PM)ThomasL Wrote: Of course if you type ABC the function int() throws an error as this cannot be converted into a number.It's a valid number in hexadecimal, so RE: TypeError: '>=' not supported between instances of 'str' and 'int' - ThomasL - Aug-16-2019 (Aug-15-2019, 09:44 PM)AsadZ Wrote: @ThomasL is there any solution for this. i am trying to build a basic calculatorShall your calculator be able to calculate with hexadecimal numbers? RE: TypeError: '>=' not supported between instances of 'str' and 'int' - AsadZ - Aug-19-2019 Simple Addition, Subtraction, Division, And Multiplication. yeah! it should also calculate with hexa-decimal. When the user input a alphabet and there should be a error that "Please Enter A Valid Number" Is it Possible or not. And i am glad to see that you are helping me RE: TypeError: '>=' not supported between instances of 'str' and 'int' - Malt - Aug-20-2019 (Aug-12-2019, 02:09 PM)AsadZ Wrote: num0 = input("Enter a Number: ") If you are very sure that you are going to get only integer then you can use something like num0 = int(input("Enter a Number: ")) RE: TypeError: '>=' not supported between instances of 'str' and 'int' - ThomasL - Aug-20-2019 Hi Asad, please have a look at this sample code: def get_valid_input(): while True: answer = input('Enter a decimal or hexadecimal number: ') if all(character in '0123456789abcdef' for character in answer.lower()): if any(character in 'abcdef' for character in answer.lower()): return answer, 16 return answer, 10 print(f'Please repeat input, only {list(allowed)} are allowed') answer, base = get_valid_input() number = int(answer, base=base) print(number) |