0

I've been doing my first calculator in Python and I have encountered small problem, let me show you a bit of my code:

fNum = input('Enter first number:' '\n')  # user gives numbers
sNum = input('Enter second number:' '\n')
if sNum.isalpha() or fNum.isalpha() is True:  # checking are they numbers
    print('You have to give me a number!')

How can I "return" to the beginning of the code and give user another chance to write down numbers? Because right now the program ends if letter or anything that isn't a number is inputted.

Thanks for help and sorry if this question is about something very easy, but I'm inexperienced yet.

Zsan
  • 3
  • 2

1 Answers1

0

This technique is called validation, use a while loop until condition is broken:

num1 = input('Enter 1st number:') 
num2 = input('Enter 2nd number:')

# while both strings are not a bunch of digits 

while not (num1.isdigit() and num2.isdigit()):
    num1 = input('Enter 1st number:') 
    num2 = input('Enter 2nd number:')
Malik Brahimi
  • 16,341
  • 7
  • 39
  • 70