Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Code by a student, with comments by me
- while True:
- print("Select operation.")
- print("1.Add : + ")
- print("2.Subtract : - ")
- print("3.Multiply : * ")
- print("4.Divide : / ")
- print("5.Power : ^ ")
- print("6.Remainder: % ")
- print("7.Terminate: # ")
- print("8.Reset : $ ")
- choice = input("Enter choice(+,-,*,/,^,%,#,$): ")
- if (choice) == "#": # () not needed around choice
- print("Done. Terminating")
- exit() # break would be better
- elif (choice) == "$":
- True # the word "True" on its own doesn't make sense
- # what are you wanting to set to True?
- # you ask the user to enter + - * / etc. but then you check for 1 to 8
- elif choice in ('1', '2', '3', '4', '5', '6', '7', '8'):
- # You should write a function for getting a float from the user
- # That function needs to do the necessary validation before attempting to construct a float
- num1 = float(input("Enter first number: "))
- num2 = float(input("Enter second number: "))
- # you need to write the functions add(), subtract() etc.
- # "select_op()" is undefined
- if (select_op(choice) == 1):
- print(num1, " + ", num2, " = ", add(num1, num2))
- elif (select_op(choice) == 2):
- print(num1, " - ", num2, " = ", subtract(num1, num2))
- elif (select_op(choice) == 3):
- print(num1, " * ", num2, " = ", multiply(num1, num2))
- elif (select_op(choice) == 4):
- print(num1, " / ", num2, " = ", divide(num1, num2))
- elif (select_op(choice) == 5):
- print(num1, " ^ ", num2, " = ", power(num1, num2))
- elif (select_op(choice) == 6):
- print(num1, " % ", num2, " = ", remainder(num1, num2))
- elif (select_op(choice) == 7):
- print(num1, " # ", num2, " = ", terminate(num1, num2))
- else:
- # "not a valid number" is confusing. Perhaps you mean "not a valid choice"
- print("Not a valid number,please enter again")
- 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