python - Issues with 'power' in coding a calculator with python3 -


i new coding, , want make calculator calculate powers, if numbers entered high program return error saying number large. therefore, wanted make if second number user inputs equal or above thirty, print "number high" , make them input new number. here code that:

if choice == '5' , num2 >= '30':     print("second number high")     num2 = float(input("enter second number: ")) 

when run , enter 30 second number input, following error message: traceback (most recent call last): file "python", line 41, in typeerror: '>=' not supported between instances of 'float' , 'str'

here code 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 calculator app!") print("select operation.") print("1.add") print("2.subtract") print("3.multiply") print("4.divide") print("5.to 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' , num2 >= '30':     print("second number 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) 

python uses different notation numbers (floats in case) vs. strings. strings '30' written inside single-quotes ('30') or double-quotes ("30"). numbers 30 or 30.0 written without quotes. trying compare number string cause error. python means when says typeerror: '>=' not supported between instances of 'float' , 'str'

try this:

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

(unrelated comment: happens if user enters number big second time well? consider using sort of loop keep requesting input until valid number entered.)


Comments

Popular posts from this blog

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

python Tkinter Capturing keyboard events save as one single string -

sql server - Why does Linq-to-SQL add unnecessary COUNT()? -