Posts: 1
Threads: 1
Joined: Nov 2017
Nov-19-2017, 09:28 PM
(This post was last modified: Nov-19-2017, 09:28 PM by kresh.)
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
Posts: 12,024
Threads: 484
Joined: Sep 2016
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
Posts: 1,298
Threads: 38
Joined: Sep 2016
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()"
If it ain't broke, I just haven't gotten to it yet.
OS: Windows 10, openSuse 42.3, freeBSD 11, Raspian "Stretch"
Python 3.6.5, IDE: PyCharm 2018 Community Edition
Posts: 606
Threads: 3
Joined: Nov 2016
Nov-21-2017, 03:36 PM
(This post was last modified: Nov-21-2017, 03:56 PM by heiner55.)
Maybe your break is wrong indented.
I assume 4 spaces back.
Posts: 86
Threads: 34
Joined: Oct 2016
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,
Posts: 12,024
Threads: 484
Joined: Sep 2016
Nov-21-2017, 09:04 PM
(This post was last modified: Nov-21-2017, 09:05 PM by Larz60+.)
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()
|