Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

Python 

No content, no purpose

Code
# I have been learning python for 3 days
# I accept criticism and accusations, I will be very happy, I will continue to grow strong
# I will continue to practice and upload the completed code to the HF forum
# Although real life is too dark and painful, but I still hope that the world can be better

def Application_1():
    print('Register Account')
    un1 = input('Registered account name: ')
    data1 = False
    while data1 == False:
        err1 = 0
        while err1 < 2 :
            up1 = input('Register account password: ')
            err1 += 1
            if len(up1) != 6:
                print('Error, 6 digits or letters are required')
                err1 -= 1
            elif err1 == 1:
                up2 = input('Confirm password again: ')
                err1 += 1
            elif up1 != up2:
                print('The second input is not the same as the first')
                err1 -= 1
            elif up1 == up2 and err1 == 2:
                print('registration success')
                err1 += 1
            if err1 == 2:
                data1 = True
                break
    data = print('account number:{0}\npassword:{1}\nPlease save the account password and other information'.format(un1,up2))
Application_1()
When you paste code here, use "code" tags around the code,
so we can get the indentation of your code ( which is very important in python..).

Now let's dive in 🕵️
First of all, use more meaningful names for your parameters,
and try to avoid numbers in them.
for example
Code
un1 = input('Registered account name: ')

# This param can be easily renamed to
acc_name = input('Registered account name: ')
# or
username = input('Registered account name: ')
and immediately your code will be much more readable and easy to maintain if needed.

For the While loop you don't need data1, because you are using break at the end,
which make data1 to be uselss, you don't need an actual parameter to make
a boolean equation
Code
data1 = False
while data1 == False:

#Can effectively become
while True:
# and use the same break at the end


When you ask for input from the user, make sure you mention all the rules,
you cant just ask for a password with no further information
Code
up1 = input('Register account password: ')

and then "punish" the user for not making it 6 digits password
Code
if len(up1) != 6:
     print('Error, 6 digits or letters are required')


The flow logic based on incremented "error" counter, is kinda mess you don't need it
as you can see, the code looks pretty complicated for a very simple task.
You can just use While True and break it whenever you need to "ensure" stuff before the next stage.

On the last line of your function you make
Code
data = print('account number:{0}\npassword:{1}\nPlease save the account password and other information'.format(un1, up2))
there is no reason to make
Code
somthing = print(...)
"print" function is a void funtion, it returns nothing, you just need to do print(..)

also use f-string, its much better than format, its more efficient and readable
its looks like that,
Code
        print(f'account number:{un1}\n'
              f'password:{up2}\n'
              f'Please save the account password and other information')
params are just inside the string instead of the end.

Here is my version for your code, nothing fancy
but you can tell its much cleaner and understanable

Code
def Application_1():
    print('Register an Account')
    username = input('Registered account name: ')

    while True:  # Password loop
        password = input('Enter 6 chars long password:')
        if len(password) == 6:
            break
        else: # You don't even need the "else" because it will break anyway if its true
            print("Password not 6 chars long")

    while True:  # Confirm Password loop
        confirm_password = input('Confirm password:')
        if password == confirm_password:
            break
        #Not using "else", we don't need that
        print("Passwords not identical")

    print(f'account number:{username}\n'
          f'password:{password}\n'
          f'Please save the account password and other information')


Hope you learned something from that,
practice makes perfect, good luck!
Dude .
Code
Application_1()

make it into

Code
if __name__ == '__main__':
     Application_1()

This is a community standard for running python scripts. So then you can use your function like this:

Code
python myscript.py


You are doing great, keep going!
(Aug 30, 2021 11:45 AM)czarnylab Wrote:  
Code
Application_1()

make it into

Code
if __name__ == '__main__':
    Application_1()

This is a community standard for running python scripts. So then you can use your function like this:

Code
python myscript.py


You are doing great, keep going!

thanks so much


Possibly Related Threads...
Thread / Author Replies Views Last Post
[Python] by HorribleaimFeb 11, 2025 06:17 PM 4 556 Feb 11, 2025 06:17 PM
Last Post: SIM-5604220
[Python] by CrouchingTig3rApr 8, 2020 09:09 PM 9 254 Apr 8, 2020 09:09 PM
Last Post: hinstera
[.NET] by Protótype_Mar 19, 2017 07:16 AM 9 468 Mar 19, 2017 07:16 AM
Last Post: AnarchyCoder