0
votes

I am very new to coding, and I want to make my calculator calculate powers, however if the numbers that are entered are too high the program will return an error saying the number is too large. Therefore, i wanted to make it so that if the second number the user inputs is equal to or above thirty, it will print "Number too high" and make them input a new number. Here is my code for that:

if choice == '5' and num2 >= '30':
    print("Second number too high")
    num2 = float(input("Enter second number: "))

When i run this and enter 30 as the second number input, i get the following error message: Traceback (most recent call last): File "python", line 41, in TypeError: '>=' not supported between instances of 'float' and 'str'

Here is all the code for my calculator:

import time 

def add(x, y):
    return x + y

def subtract(x, y):
    return x - y

def multiply(x, y):
    return x * y

def divide(x, y):
    return x / y

def power(x, y):
    return x ** y

print("Welcome to the Calculator App!")
print("Select operation.")
print("1.Add")
print("2.Subtract")
print("3.Multiply")
print("4.Divide")
print("5.To the Power of")

choice = input("Enter choice(1, 2, 3, 4 or 5):")

while choice not in ("1","2","3","4","5"):
    print("Invalid Input")
    choice = input("Enter choice(1, 2, 3, 4 or 5):")

num1 = float(input("Enter first number: "))
num2 = float(input("Enter second number: "))

if choice == '5' and num2 >= '30':
    print("Second number too high")
    num2 = float(input("Enter second number: "))

if choice == '1':
    print(num1,"+",num2,"=", add(num1,num2))

elif choice == '2':
    print(num1,"-",num2,"=", subtract(num1,num2))

elif choice == '3':
    print(num1,"*",num2,"=", multiply(num1,num2))

elif choice == '4':
    print(num1,"/",num2,"=", divide(num1,num2))

elif choice == '5':
    print(num1,"**",num2,"=", power(num1,num2))

else:
    print("Invalid input")

time.sleep(10)
2

2 Answers

0
votes

Python uses different notation for numbers (floats in this case) vs. strings. Strings like '30' are written inside single-quotes ('30') or double-quotes ("30"). Numbers like 30 or 30.0 are written without quotes. Trying to compare a number with a string will cause an error. That is what Python means when it says TypeError: '>=' not supported between instances of 'float' and 'str'

Try something like this:

if choice == '5' and num2 >= 30:

(Unrelated comment: What happens if the user enters a number that is too big the second time as well? Consider using some sort of loop to keep requesting input until a valid number is entered.)

0
votes

Change this if statement

if choice == '5' and num2 >= '30':
    print("Second number too high")
    num2 = float(input("Enter second number: "))

to a while loop
like this

while choice == '5' and num2 >= 30:
    print "invalid input - enter a number < 30"
    num2 = float(input("Enter 2nd number: "))

Now you are testing num2 against a numeric value and re-requesting a value for num2 until you receive one below 30