I would like for the input to remember the first number given to it even if you type something silly on the second input. While right now obviously the loop starts again and asks for both of the inputs again. Is there an easy way to do this?
Calculator
Give a number: 124
Give a number: dwa
This input is invalid.
Give a number: 12
Give a number:
while I would like it to do this:
Calculator
Give first number: 124
Give second number: dwa
This input is invalid.
Give second number: 12
And this is the entire code:
import math
import sys
print("Calculator")
def main():
while True:
try:
kek1 = int(input('Give a number: '))
kek2 = int(input('Give a number: '))
while True:
print('(1) +')
print('(2) -')
print('(3) *')
print('(4) /')
print('(5) sin(number1/number2')
print('(6) cos(number1/number2)')
print('(7) Change numbers')
print('(8) Quit')
print(f'Current numbers are: {kek1} {kek2}')
kaka = input("Please select something (1-6):")
plus = kek1 + kek2
minus = kek1 - kek2
times = kek1 * kek2
divide = kek1 / kek2
sin = math.sin(kek1/kek2)
cos = math.cos(kek1/kek2)
if kaka == "1":
print(f'The result is: {plus}')
if kaka == "2":
print(f'The result is: {minus}')
if kaka == "3":
print(f'The result is: {times}')
if kaka == "4":
print(f'The result is: {divide}')
if kaka == "5":
print(f'The result is: {sin}')
if kaka == "6":
print(f'The result is: {cos}')
if kaka == "7":
kek1 = int(input('Give a number: '))
kek2 = int(input('Give a number: '))
if kaka == "8":
print("Thank you!")
sys.exit(0)
except SystemExit:
sys.exit(0)
except:
print("This input is invalid.")
if __name__=="__main__":
main()
If you have any ideas on how to do this it would be a great help.
Use separate try statements for each input with their own loops
while True:
try:
kek1 = input("First number")
kek1 = int(kek1)
break
except ValueError:
print("Invalid")
continue
while True:
try:
kek2 = input("Second number")
kek2 = int(kek2)
break
except ValueError:
print("Invalid")
continue
And then go into the rest of your loop. Sorry for a brief answer but I'm on my phone :P