Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Data Science by (18.4k points)

I am trying to loop the input of the user until a correct value is equal to one item in the list.

name = ['peter', 'jay']

user = str(input("Enter UserID: "))

if user == name:

print ("welcome back" + user")

else:

print ("wrong input, you have 3 more tries, please try again")

1 Answer

0 votes
by (36.8k points)

You need to code like this:

name = ['peter', 'jay']

for i in range(0,4):

    user = str(input("Enter UserID: "))

    if user in name: 

        print ("welcome back " + user)

        break

    elif i !=3: 

        print ("wrong input, you have", (3-i) , "more tries, please try again")

    else: 

         print ("wrong input, you have no more chances")

 Do check out data science with python certification to understand from scratch

Browse Categories

...