0

I'm new to Python and have limited understanding. When I execute my code it doesn't print anything out.

If anyone needs more details, please let me know:

def main():
    login()

def login():
    username="john"
    password="apple"
    print("Enter username:")
    answer1=input()
    print("Enter password:")
    answer2=input()
    if answer1==username and answer2==password:
        print("Welcome - Access Granted")
        menu()

def menu():
    print("************MAIN MENU**************")
JulienD
  • 7,102
  • 9
  • 50
  • 84
Jack123
  • 5
  • 2

3 Answers3

2

a1 and a2 outside functions are called variables. So variable a1 will contain username and variable a2 will contain password if the user type and enter the data.

Once the function main is being called, it will return function login which is now carrying the arguments a1 and a2. Don't be confused, variables a1 and a2 are now arguments.

Once a function is being called, the data inside are arguments. Once we create a function, the data inside are parameters. The parameters answer1 and answer2 can only be use inside the function login.

Our purpose of storing data outside the function is that we can use them to another functions we created in the future. :)

def main():
    return login(a1,a2)

def login(answer1, answer2):
    username = "john"
    password = "apple"
    if answer1==username and answer2==password:
        print("Welcome - Access Granted.")
        menu()
    else:
        pass

def menu():
    print("***MAIN MENU***")


print("Enter username:")
a1 = input()

print("Enter password:")
a2 = input()

main()
  • the user asking the question is obviously new. You're answer doesn"t give any feedback on why he should use your code. – scharette Oct 24 '17 at 14:27
  • "try this" is rarely good enough. Except for the cases where advanced python users are directly asking for working code. – scharette Oct 24 '17 at 14:28
  • Okay, well for the OP I'm just giving him a new ideal solution relating to his problem. I love sharing new ideas :) –  Oct 24 '17 at 14:42
  • It is a really good thing to do that, maybe you could update your answer to give feedback on why you propose this solution ? – scharette Oct 24 '17 at 14:44
  • I hope my explanations are useful. :) –  Oct 24 '17 at 15:29
1

You defined all your functions well, but you never call them. So you're telling the interpreter here is what every functions does but dont call them yet. This result is doing nothing.

Here is an example where I call main():

def main():
    login()

def login():
    username="john"
    password="apple"
    print("Enter username:")
    answer1=input()
    print("Enter password:")
    answer2=input()
    if answer1==username and answer2==password:
        print("Welcome - Access Granted")
        menu()

def menu():
    print("************MAIN MENU**************")

main()
scharette
  • 9,437
  • 8
  • 33
  • 67
  • Thanks for the help, this was very useful! – Jack123 Oct 24 '17 at 13:53
  • @Jack123 No problem! Please accept the answer if it was helpful to you. it is a good pratice to accept helpful anwers in order to close questions. Welcome to SO! – scharette Oct 24 '17 at 13:55
  • And then you write another script that wants to import a function from here, and `main()` gets called. – JulienD Oct 24 '17 at 14:27
  • I think the goal here was to show him why nothing was happening. Obviously, he is new and didn't know about functions call. If you want to proposed an alternative, post an answer with an entire new code. – scharette Oct 24 '17 at 14:31
  • Well there is plenty of shortcomings in the code you posted also... There is always a better a version. But from what the user wanted to understand, I think my answer was good enough. – scharette Oct 24 '17 at 14:34
  • I agree, just saying, why not make it right from the start. – JulienD Oct 24 '17 at 14:41
  • For the same reason you didn't provide an answer based on oriented-object programming using polymorphism. _Just barely good enough_ – scharette Oct 24 '17 at 18:45
1

This is safer, because you can execute the file as well as importing it without executing the main(). It is the standard way of creating an executable script.

def main():
    login()

def login():
    username="john"
    password="apple"
    print("Enter username:")
    answer1=input()
    print("Enter password:")
    answer2=input()
    if answer1==username and answer2==password:
        print("Welcome - Access Granted")
        menu()

def menu():
    print("************MAIN MENU**************")

if __name__ == "__main__":
    main()

Read more about it there: what-does-if-name-main-do

JulienD
  • 7,102
  • 9
  • 50
  • 84