![]() |
Python 3 break statement - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Python 3 break statement (/thread-6390.html) |
Python 3 break statement - kresh - Nov-19-2017 Hi everyone. I have some problem with BREAK in my code. I don't understand why the program does not stop after adding a new username to LIST. Sorry for my question, but I'm a new in Python :) Thanks in advance #find out an available username # -*- coding: utf-8 -*- print("\n===========Sing Up ====================") print("\nPlease, enter new username:") print("\n Username: ") current_users=['vlad','kate','tania','mike','david'] bad_symbols=('@','#','$','%','^','&','*','(',')','-,'_','+','=','<','>','|',']','[','{','}'',','.','/','?') while True: name_user=raw_input() name_user=name_user.lower() alfabet=list(name_user) for alfa in alfabet: if alfa in bad_symbols: print("\nSorry it is a special symbols, please try again") else: if name_user in current_users: print("\n Sorry,but " + str(name_user)+" the name is not available" ) else: print("your USERNAME=" +str(name_user)+" is approved") current_users.append(name_user) print(current_users) break RE: Python 3 break statement - Larz60+ - Nov-19-2017 It would be much easier to test for good symbols in name_user, this can be done first, make sure you import re then test name_user for only numbers and letters: if re.match("^[A-Za-z0-9_-]*$", name_user): # user ok add ok code here else: # Not OK, add your error code here RE: Python 3 break statement - sparkz_alot - Nov-21-2017 Why does your thread title say Python 3 when this: (Nov-19-2017, 09:28 PM)kresh Wrote: name_user=raw_input() is Python 2, in Python 3 it would be "name_user = input()" RE: Python 3 break statement - heiner55 - Nov-21-2017 Maybe your break is wrong indented. I assume 4 spaces back. RE: Python 3 break statement - iFunKtion - Nov-21-2017 In your code, you have nested an if statement within an else statement, if, elif, else would make your break statement work: if alfa in bad_symbols: print("\nAlphanumeric values only") elif name_user in current_users: print("\nName already in use") else: print("Your USERNAME=" +str(name_user)+" is approved") current_users.append(name_user) print(current_users) break [code] [/code]the break statement should break out of the loop once you have created a new user name. Nested if statements can be really confusing, and there is usually a better way to approach the logic. With if, elif, else, you can add as many elifs as you want, though it is good practice to keep it to one elif within an if else block, if there are more, then a switch/case statement is much easier to work with and read. And as stated earlier, raw_input() is python2, just input() is python3 syntax. Regular Expression would indeed be a better and quicker way to do this, RE: Python 3 break statement - Larz60+ - Nov-21-2017 Try this: #find out an available username # -*- coding: utf-8 -*- import re current_users=['vlad','kate','tania','mike','david'] def get_user(): while True: name_user = raw_input('Please, enter new username: ') if not re.match("^[A-Za-z0-9_-]*$", name_user): print("\nSorry it is a special symbols, please try again") continue elif name_user in current_users: print("\n Sorry,but " + str(name_user) + " the name is not available") continue print("your USERNAME=" + str(name_user) + " is approved") current_users.append(name_user) print('current_users: {}'.format(current_users)) break if __name__ == '__main__': get_user() |