简体   繁体   中英

Variable and Function help // Python

Alright so here's the code

def user_password():
input('Please Enter Password: ')

#profiles
def intercept():
    user_password()
    if user_password == "intercept":
            print("this is working so far")
    else:
            print("Incorect Password")
def Jimmy():
    user_password()
    if user_password == "Jimmy":
            print("this is working so far")
    else:
            print("Incorect Password")
def Tommy():
    user_password()
    if user_password == "Tommy":
            print("this is working so far")
    else:
            print("Incorect Password")

#login
user_functions = dict(
    intercept=intercept,
    Jimmy=Jimmy,
    Tommy=Tommy,
    # ...
)
 user_input = input("Input function name: ") 

if user_input in user_functions:
    user_functions[user_input]()
else:
    print("Error: Unknown function.")

PROBLEMS:

  • My code always starts with asking for the password even though I don't want it to.
    • When I change the first variable to a function it fixes this
    • Why does it execute when I'm just setting the variable. I'm pretty sure I shouldn't have to use a function instead of a variable
  • No matter what it always ends up as Incorrect Password even if I give the correct password

I think you are trying to write something like that:

def get_user_password():
   return input('Please Enter Password: ')


def Jimmy():
    user_password = get_user_password()
    if user_password == "Jimmy":
            print("this is working so far")

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