Python Forum
TypeError: '>=' not supported between instances of 'str' and 'int'
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
TypeError: '>=' not supported between instances of 'str' and 'int'
#1
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
Reply
#2
if not int(num0) >= 0:
Reply
#3
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.
Reply
#4
(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.
Reply
#5
(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
Reply
#6
(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?
Reply
#7
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
Reply
#8
(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: "))
Reply
#9
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)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Sort Function: <' not supported between instances of 'float' and 'tuple' quest 2 8,060 Apr-30-2021, 07:37 PM
Last Post: quest
Exclamation TypeError: '>=' not supported between instances of 'int' and 'str' helpme1 11 8,794 Mar-11-2021, 11:13 AM
Last Post: helpme1
  Type error: '>' not supported between instances of 'NoneType' and 'int' spalisetty06 1 10,476 Apr-29-2020, 06:41 AM
Last Post: buran
  TypeError: '<' not supported between instances of 'str' and 'int' Svensation 5 8,796 Jan-20-2020, 08:12 PM
Last Post: buran
  TypeError: Not supported between instances of 'function' and 'int' palladium 9 19,602 Dec-06-2019, 12:40 AM
Last Post: palladium
  '>' not supported between instances of 'str' and 'int' graham23s 2 3,969 May-11-2019, 07:09 PM
Last Post: micseydel
  Newbie Question re "TypeError: '<' not supported between instances of 'list' and 'int sr12 8 13,056 Apr-11-2019, 08:19 PM
Last Post: sr12
  '<' not supported between instances of 'str' and 'int' jayaherkar 1 7,932 Apr-09-2019, 03:25 PM
Last Post: perfringo
  TypeError: '<' not supported between instances of 'int' and 'builtin_function_or_meth yann2771 1 5,549 Mar-05-2019, 09:51 PM
Last Post: stranac
  '<=' not supported between instances of 'str' and 'int' Loom 1 7,772 Aug-11-2018, 07:34 PM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020