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)