简体   繁体   中英

Python - User defined Functions

I am trying to convert code(that registers a user) to a function, but instead it gives output = None instead of a concatenated string (new_user_name + ", " + new_password). Can anyone enlighten me as to what mistake am I making, the code itself already works when its not a function.

def reg_user ():
    
    #Input from the admin to enter the user name and password for the new user
            
    new_user_name = input("Enter new Username: ").strip()
            
    new_password = input("Enter new Password: ").strip()
            
    confirm_new_password = input("Re-enter new Password: ").strip() #Password confirmation
            
    while new_password != confirm_new_password:
                
        print("Password does not match. Ensure you have put the same password")
                
        print()
                
        new_password = input("Enter new Password: ").strip()
                
        confirm_new_password = input("Re-enter new Password: ").strip()
                
        if new_password == confirm_new_password:
                
            return (new_user_name + ", " + new_password)

new_user = reg_user()
print(new_user)

OUTPUT = None

Desired output = eg. (Sanele, 595dasdf)

You have made a small error with your indentation:

def reg_user ():
    
    #Input from the admin to enter the user name and password for the new user
            
    new_user_name = input("Enter new Username: ").strip()
            
    new_password = input("Enter new Password: ").strip()
            
    confirm_new_password = input("Re-enter new Password: ").strip() #Password confirmation
            
    while new_password != confirm_new_password:
                
        print("Password does not match. Ensure you have put the same password")
                
        print()
                
        new_password = input("Enter new Password: ").strip()
                
        confirm_new_password = input("Re-enter new Password: ").strip()
                
                
    return (new_user_name + ", " + new_password) # Has to be one tab less

new_user = reg_user()
print(new_user)

OUTPUT = None

# Desired output = eg. (Sanele, 595dasdf)

As others have said, you can fix your script simply by reducing the if statement identation one level.

However, your code have some bad coding practices, like it is WET (you Write Everything Twice ) instead of keeping it DRY ( Don't Repeat Yourself ). Here you have my version:

def reg_user ():

    new_user_name = input("Enter new Username: ").strip()
    while True:
        new_password = input("Enter new Password: ").strip()
        confirm_new_password = input("Re-enter new Password: ").strip()
        
        if new_password == confirm_new_password:
            return new_user_name + ", " + new_password
        
        print("Password does not match. Ensure you have put the same password")


new_user = reg_user()
print(new_user)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM