Mar-21-2021, 07:17 PM
Also, if you see a lot of repetitive code (like here), take a step back to see how things can be combined - often in a function, but even easier here. Combining what has been stated here
The while loop that goes back until the user enters properly formatted numbers
The use of break and continue
The use of f strings rather than .format
Minimizing repetitious code
#User inputs math type operation = input(''' Please type in the math operation you would like to complete: + for addition * for multiplication ''') #User inputs number while True: number_str = input('Enter your numbers, separate by comma: ') try: numbersstrings = number_str.split(sep=',') n1 = float(numbersstrings[0]) n2 = float(numbersstrings[1]) except Exception: print("You have not followed instructions!") continue break print(f'{n1} {operation} {n2}') if operation == '*': print(n1*n2) if operation == "+": print(n1+n2)Now this still is not great - I agree with Dean that you should allow the user to enter the operation at the same time as the numbers, but this illustrates a number of items/learning points -
The while loop that goes back until the user enters properly formatted numbers
The use of break and continue
The use of f strings rather than .format
Minimizing repetitious code