Python Forum

Full Version: TypeError: '>=' not supported between instances of 'str' and 'int'
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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
if not int(num0) >= 0:
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.
(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.
(Aug-12-2019, 07:30 PM)ndc85430 Wrote: [ -> ]
(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.
@ThomasL is there any solution for this. i am trying to build a basic calculator
(Aug-15-2019, 09:44 PM)AsadZ Wrote: [ -> ]@ThomasL is there any solution for this. i am trying to build a basic calculator
Shall your calculator be able to calculate with hexadecimal numbers?
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
(Aug-12-2019, 02:09 PM)AsadZ Wrote: [ -> ]num0 = input("Enter a Number: ")
if not(num0>=0):
    print("Please enter a Valid Number >=0.")
else:
    print(num0)

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