Хм, возможно, есть и более питонический способ, но он делает это:
while True:
try:
num1 = int(input("Give first Integer: "))
break
except ValueError:
print("You need Integer!")
while True:
try:
num2 = int(input("Give second Integer: "))
break
except ValueError:
print("You need Integer!")
Пример вывода:
Give first Integer: R
You need Integer!
Give first Integer: 4
Give second Integer: NO
You need Integer!
Give second Integer: Nope
You need Integer!
Give second Integer: -12
Подход 2 с одним циклом while:
valid = []
while True:
try:
num = int(input("Give an Integer: "))
valid.append(num)
if len(valid) == 2:
break
except ValueError:
print("You need Integer!")
num1, num2 = valid
print("The entered integers are: ", num1, num2)
Пример вывода:
Give an Integer: NOOO
You need Integer!
Give an Integer: 45
Give an Integer: again?
You need Integer!
Give an Integer: okay
You need Integer!
Give an Integer: 4
The entered integers are: 45 4