Python Forum
No again function discovered in python
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
No again function discovered in python
#1
This is my first semi-professional project in Python. And I'm in trouble.

Python does not understand the again function. This means that when you press button 2, Python executes the second function and after finish it, python do that function agin.

Can anyone help me solve this problem?

This is my code :
import subprocess
#do it again
def again():
    again_v = input('For Menu type (M) for Exit type (E) : ')
    if again_v.upper() == 'M':
        start()
    elif again_v.upper() == 'E':
        print('Good Luck !')
    else:
        again()

    
#create username and password
def creat_user():
    user = input('Enter your Username : ')
    Pass = input('Enter your Password : ')
    userpass = subprocess.check_output('net user {} {} /add' . format(user,Pass) , shell=True)
    adm = subprocess.check_output('net localgroup administrators {} /add' . format(user) , shell=True) #give administrator to user
    print('Done !')  


#change password        
def ch_pass():
    u = input('Ok, Enter your Username : ')
    p = input('Now, Enter your new Password : ')
    u_p = subprocess.check_output('net user {} {}' . format(u,p) , shell=True)
    print('Action completed !')
     


def start():
    a = input('''
Welcome,
[+] (1) Create new user
[+] (2) Change your password

what do you want to do? : ''')
    
    a = int(a)


    if a == 1:
        creat_user()

    elif a == 2:
        ch_pass()
 

    creat_user()
    again()
        

    ch_pass()
    again()

start()
snippsat write Apr-02-2021, 09:57 PM:
Added code tag in your post,look at BBCode on how to use.
Reply
#2
Try a structure like this.
So the point here is that always fall back to menu() function,
here can do new action or quit out.
See that there is no use of exit as return to this job.
def creat_user():
    pass

def ch_pass():
    while True:
        p = input('Now, Enter your old Password : ')
        if p == '42':
            return 'Action completed!\n'
        else:
            print('Wrong old password,try again')

def menu():
    while True:
        print('(1) Create user')
        print('(2) Change your password')
        print('(Q) Quit')
        choice = input('Enter your choice: ').lower()
        if choice == '1':
            print('Not implemented yet\n')
        elif choice == '2':
            print(ch_pass())
        elif choice == 'q':
            return
        else:
            print(f'Not a correct choice: {choice},try again\n')

if __name__ == '__main__':
    menu()
Reply
#3
This strikes me as odd: using recursion to simulate iteration. The problem with recursion is that it can be limited by stack depth. I consider this particularly bad code.
Reply
#4
Think about the flow. You do start(), get the input and either create user id or change password. After doing that, it returns to the start() function where you create a user again (line 49) then call the again() function. After messaging the user, you either recursively do the start() function again or give a message and the again() function returns. If the user typed E it returns to the start() function again at line 53, calls the change password function, then returns from that to line 54 which again calls the again() function. This is after they have tried to exit the program. They try a second time to exit the program with an E and finally exit.

I do not think this is the flow you want.
Reply


Forum Jump:

User Panel Messages

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