Advertisement
acclivity

pyExercise

May 18th, 2022
613
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.30 KB | None | 0 0
  1. # Code by a student, with comments by me
  2.  
  3. while True:
  4.     print("Select operation.")
  5.     print("1.Add      : + ")
  6.     print("2.Subtract : - ")
  7.     print("3.Multiply : * ")
  8.     print("4.Divide   : / ")
  9.     print("5.Power    : ^ ")
  10.     print("6.Remainder: % ")
  11.     print("7.Terminate: # ")
  12.     print("8.Reset    : $ ")
  13.     choice = input("Enter choice(+,-,*,/,^,%,#,$): ")
  14.     if (choice) == "#":                                 # () not needed around choice
  15.         print("Done. Terminating")
  16.         exit()                                          # break would be better
  17.     elif (choice) == "$":
  18.         True                                            # the word "True" on its own doesn't make sense
  19.                                                         # what are you wanting to set to True?
  20.  
  21.     # you ask the user to enter + - * / etc. but then you check for 1 to 8
  22.     elif choice in ('1', '2', '3', '4', '5', '6', '7', '8'):
  23.  
  24.         # You should write a function for getting a float from the user
  25.         # That function needs to do the necessary validation before attempting to construct a float
  26.         num1 = float(input("Enter first number: "))
  27.         num2 = float(input("Enter second number: "))
  28.  
  29.         # you need to write the functions add(), subtract() etc.
  30.         # "select_op()" is undefined
  31.         if (select_op(choice) == 1):
  32.             print(num1, " + ", num2, " = ", add(num1, num2))
  33.         elif (select_op(choice) == 2):
  34.             print(num1, " - ", num2, " = ", subtract(num1, num2))
  35.         elif (select_op(choice) == 3):
  36.             print(num1, " * ", num2, " = ", multiply(num1, num2))
  37.         elif (select_op(choice) == 4):
  38.             print(num1, " / ", num2, " = ", divide(num1, num2))
  39.         elif (select_op(choice) == 5):
  40.             print(num1, " ^ ", num2, " = ", power(num1, num2))
  41.         elif (select_op(choice) == 6):
  42.             print(num1, " % ", num2, " = ", remainder(num1, num2))
  43.         elif (select_op(choice) == 7):
  44.             print(num1, " # ", num2, " = ", terminate(num1, num2))
  45.         else:
  46.             # "not a valid number" is confusing. Perhaps you mean "not a valid choice"
  47.             print("Not a valid number,please enter again")
  48.  
  49. exit()              # As I understand it, exit() is not always available. You could just omit this line.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement