Python: Simple calculator (WET to DRY) | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Python: Simple calculator (WET to DRY)

Hey guys, i'm wondering if you could help take a look at this code, is there anything i should do to make it more DRY? while True: print("Enter 'add' to add two numbers") print("Enter 'subtract' to subtract two numbers") user_input = input(": ") x = (user_input != ("add" or "subtract")) if x == True: while x: print("Try Again") print("Enter 'add' to add two numbers") print("Enter 'subtract' to subtract two numbers") user_input = input(": ") if user_input == "add": break if user_input == "subtract": break def add_subtract (y): print("what numbers would you like to " + y +" ?") num_1= int(float (input("first number: "))) num_2= int(float (input("second number: "))) if y == "add": print("Your answer is: " + str(num_1 + num_2)) if y == "subtract": print("Your answer is: " + str(num_1 - num_2)) add_subtract (user_input) print("More numbers for me? Yes/No") add_user_input = input(": ") if add_user_input == "No": break if add_user_input == "Yes": continue if add_user_input != "No" and "Yes": while True: print ("Please insert a proper response") print("More numbers for me? Yes/No") add_user_input = input(": ") if add_user_input == "No": break if add_user_input == "Yes": break if add_user_input == "Yes": continue else: break

11th Aug 2017, 2:16 PM
Joshua Ng
Joshua Ng - avatar
1 Answer
0
you may want to something like this repeat = 0 while repeat == 0: (whatever u want) okay = int(input("u want using this calculator again? ")) okay = repeat and never give up to coding.its like a apollo rocket launch.slow at earth, but when reach space it will so fast. TRY HARDER!!!
19th Aug 2017, 7:27 AM
Kevin AS
Kevin AS - avatar