0

i have searched for a simple, non complicated answer on how to ensure that the user is asked again if they enter anything other than text for 1 and an integer for 2.

when entering these input variables, however i have only found complicated solutions that my teacher wont acccept.

is it possible if anyone can provide me with simple solutions on how to validate these variables. so far all i know how to validate is to use "while not in" functions, which only works for specific options. I am new to python, so please explain in a simple manner. thanks! :)

1-studentname=input("what is your name?:") 
2-print("what is 10+10?:")
3-studentanswer=int(input("insert answer:"))
Rana
  • 1,675
  • 3
  • 25
  • 51
ozzie
  • 11
  • 1

2 Answers2

0

You can use a while loop with if , else in it

while(true):
   t = int(input());
   if t == 1:
     # do whatever --> break at the end
   else if t == 2:
     # do whatever ---> break at the end
   else:
     continue
Priyansh Goel
  • 2,660
  • 1
  • 13
  • 37
0

You can use the .isalpha and .isdigit method. For example,

studentname=input("what's your name) ?
studentname.isalpha()

That checks whether the string consists of alphabetic characters only. Both .isalpha and .isdigit return boolean, so you can use an if condition then.

A. Attia
  • 1,630
  • 3
  • 20
  • 29