Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python 3 break statement
#1
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
Reply
#2
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
Reply
#3
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
Reply
#4
Maybe your break is wrong indented.
I assume 4 spaces back.
Reply
#5
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,
Reply
#6
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()
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Problem with a 'break' statement. christopher3786 3 2,421 Jun-20-2020, 10:16 AM
Last Post: pyzyx3qwerty
  Why doesn't my loop work correctly? (problem with a break statement) steckinreinhart619 2 3,184 Jun-11-2019, 10:02 AM
Last Post: steckinreinhart619

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020