I recently started trying to learn how to code and I was excited that I was putting together a super basic calculator, but I have painted myself into a corner that I do not know how to get out of, due mainly to my lack of knowledge. I am using the try: except: block to prevent people from typing in letters but doing so prevents me from being able to type q to quit.
answer = 0
while True:
print(f"Give me a number: (Saved Value: {answer})\n('q' to quit)")
try:
num1 = float(input("> "))
except ValueError:
if num1 == 'q':
break
else:
print("Numbers only please!\n")
continue
print(f"Give me another number: (Saved Value: {answer})\n('q' to quit)")
try:
num2 = float(input("> "))
except ValueError:
if num2 == 'q':
break
else:
print("Numbers only please!\n")
continue
print(f"Give me an operator: (+, -, /, *) (Saved Value: {answer})\n('q' to quit)")
my_math = input("> ")
if my_math == 'q':
break
if my_math == '+':
print(num1 + num2)
answer = num1 + num2
answer = answer
elif my_math == '-':
print(num1 - num2)
answer = num1 - num2
answer = answer
elif my_math == '/':
print(num1 / num2)
answer = num1 / num2
answer = answer
elif my_math == '*':
print(num1 * num2)
answer = num1 * num2
answer = answer