簡體   English   中英

變量和函數幫助// Python

[英]Variable and Function help // Python

好吧,所以這是代碼

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.")

問題:

  • 我的代碼總是從詢問密碼開始,即使我不想要它。
    • 當我將第一個變量更改為函數時,它會修復此問題
    • 當我只是設置變量時,為什么它會執行。 我很確定我不應該使用函數而不是變量
  • 即使我提供正確的密碼,無論它總是以不正確的密碼結束

我想你正在嘗試寫這樣的東西:

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")

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM