Help in Understanding While loops /u/WillingnessPast5294 Python Education

Ok so, I was just doing some coding exercise when I came into this problem.

Here’s the code:

users = ['Bob', 'Michelle', 'Rock', 'Brick', 'Jeff'] choice = '' while choice != 'quit' or 'Quit': print('What user would you like to search?n') choice = input() if choice.isdigit(): if choice == '0': print('there aint no 0th account dummy') name = int(choice) - 1 if int(choice) <= len(users): if int(choice) > 0: print(users[name]) else: print('That person does not exist') else: choice = choice.title() if choice in users: name = users.index(choice) print(users[name]) else: print('that no exist bro') 

The while loop is supposed to end when the user inputs the word ‘quit’ but it doesn’t seem to be working. So I thought it was because the title() function was turning it into ‘Quit’ so I put the or operator, but it still didn’t end the loop. The solution I came up with was this:

 elif choice != 'quit': choice = choice.title() if choice in users: name = users.index(choice) print(users[name]) else: print('that no exist bro') 

I put an elif statement to prevent the ‘quit’ from being applicable to the condition. But why did this work? Isn’t it supposed to kill the loop once the user types ‘quit’/’Quit’. How does ending a loop work? Does it need to not be applicable to any condition for it to end the loop?

submitted by /u/WillingnessPast5294
[link] [comments]

​r/learnpython Ok so, I was just doing some coding exercise when I came into this problem. Here’s the code: users = [‘Bob’, ‘Michelle’, ‘Rock’, ‘Brick’, ‘Jeff’] choice = ” while choice != ‘quit’ or ‘Quit’: print(‘What user would you like to search?n’) choice = input() if choice.isdigit(): if choice == ‘0’: print(‘there aint no 0th account dummy’) name = int(choice) – 1 if int(choice) <= len(users): if int(choice) > 0: print(users[name]) else: print(‘That person does not exist’) else: choice = choice.title() if choice in users: name = users.index(choice) print(users[name]) else: print(‘that no exist bro’) The while loop is supposed to end when the user inputs the word ‘quit’ but it doesn’t seem to be working. So I thought it was because the title() function was turning it into ‘Quit’ so I put the or operator, but it still didn’t end the loop. The solution I came up with was this: elif choice != ‘quit’: choice = choice.title() if choice in users: name = users.index(choice) print(users[name]) else: print(‘that no exist bro’) I put an elif statement to prevent the ‘quit’ from being applicable to the condition. But why did this work? Isn’t it supposed to kill the loop once the user types ‘quit’/’Quit’. How does ending a loop work? Does it need to not be applicable to any condition for it to end the loop? submitted by /u/WillingnessPast5294 [link] [comments] 

Ok so, I was just doing some coding exercise when I came into this problem.

Here’s the code:

users = ['Bob', 'Michelle', 'Rock', 'Brick', 'Jeff'] choice = '' while choice != 'quit' or 'Quit': print('What user would you like to search?n') choice = input() if choice.isdigit(): if choice == '0': print('there aint no 0th account dummy') name = int(choice) - 1 if int(choice) <= len(users): if int(choice) > 0: print(users[name]) else: print('That person does not exist') else: choice = choice.title() if choice in users: name = users.index(choice) print(users[name]) else: print('that no exist bro') 

The while loop is supposed to end when the user inputs the word ‘quit’ but it doesn’t seem to be working. So I thought it was because the title() function was turning it into ‘Quit’ so I put the or operator, but it still didn’t end the loop. The solution I came up with was this:

 elif choice != 'quit': choice = choice.title() if choice in users: name = users.index(choice) print(users[name]) else: print('that no exist bro') 

I put an elif statement to prevent the ‘quit’ from being applicable to the condition. But why did this work? Isn’t it supposed to kill the loop once the user types ‘quit’/’Quit’. How does ending a loop work? Does it need to not be applicable to any condition for it to end the loop?

submitted by /u/WillingnessPast5294
[link] [comments] 

Leave a Reply

Your email address will not be published. Required fields are marked *