0

I wrote a calculator using python and whiles ending the problem i used the break function on the while loop to break out of the loop if the user inputs stop. i also gave a condition saying that if the user types helps, it should display the help tips and a link to where he/she can learn more, but then when i input stop it does what the help input should be doing and the help input does the same thing i told it to do (give tips in how to use it.) am sharing the code down here

while True:  
  selecting_operator = input('Select Operator: ')
  if selecting_operator == 'HELP' or 'Help' or 'help':
    print(info)
    print('You can visit https://www.w3schools.com/python/python_operators.asp To learn more about Python Operators.')
  elif selecting_operator == "+":
    def addition(num1,num2):
      add = num1 + num2
      return add
    num1 = input('[+] num: ')
    num2 = input('[+] num: ')
    print(addition(num1=float(num1),num2=float(num2)))
  elif selecting_operator == "-":
    def substraction(num1,num2):
      sub = num1 - num2
      return sub
    num1 = input('[-] num: ')
    num2 = input('[-] num: ')
    print(substraction(num1=float(num1),num2=float(num2)))
  elif selecting_operator == "*":
    def multiply(num1,num2):
      mult = num1 * num2
      return mult
    num1 = input('[*] num: ')
    num2 = input('[*] num: ')
    print(multiply(num1=float(num1),num2=float(num2)))
  elif selecting_operator == "/":
    def division(num1,num2):
      dev = num1 / num2
      return dev
    num1 = input('[/] num: ')
    num2 = input('[/] num: ')
    print(division(num1=float(num1),num2=float(num2)))
  elif selecting_operator == "%":
    def modulus(num1,num2):
      mod = num1 % num2
      return mod
    num1 = input('[%] num: ')
    num2 = input('[%] num: ')
    print(modulus(num1=float(num1),num2=float(num2)))
  elif selecting_operator == "**":
    def exponet(num1,num2):
      exp = num1 ** num2
      return exp
    num1 = input('[**] num: ')
    num2 = input('[**] num: ')
    print(exponet(num1=float(num1),num2=float(num2)))
  elif selecting_operator == "//":
    def floor_division(num1,num2):
      f_div = num1 // num2
      return f_div 
    num1 = input('[//] num: ')
    num2 = input('[//] num: ')
    print(floor_division(num1=float(num1),num2=float(num2)))
  elif selecting_operator == 'STOP' or 'Stop' or 'stop':
      break

I tried moving the stop statement up but got the same result, i kept it in the middle but got the same result

Genius99
  • 1
  • 2
  • Python is not English. Try `elif selecting_operator.lower() == 'stop':` – Mark Tolonen Dec 01 '22 at 21:20
  • Thank You soo Much, it did work. I learnt that u could use the .lower but i never knew the brackets came with it, thats why i never used it – Genius99 Dec 01 '22 at 21:27
  • Non-empty strings are considered "truthy" so you had, basically, `elif selecting_operator == 'STOP' or True or True:` which is always true. – Mark Tolonen Dec 01 '22 at 21:27

0 Answers0