【问题标题】:Input inside a Function [duplicate]函数内的输入[重复]
【发布时间】:2017-10-10 13:34:09
【问题描述】:

我做了一个ATM功能程序,我需要帮助,因为我无法在我的程序中显示我的用户名(un)和密码(pw),错误是

NameError: name 'un' is not defined

我尝试在第一行定义 un ,但用户输入信息后该值没有改变。

#functions
def FWelcome():
    print("WELCOME!")
    print("Please Log-In to access ATM:")
    return;

def FUsername():
    while True:
        un=input("\nEnter Username ( use only letters ):")
        if un.isalpha():
            break
        else : #invalid
            print ("Invalid Username. Use of symbols and/or numbers are not allowed.\n")
    return;

def FPassword():
    while True:
        pw=input("Enter Password ( use only numbers ):")
        if pw.isnumeric():
            break
        else : #invalid
            print ("Invalid Password. Use of symbols and/or letters are not allowed.\n")
    return;

#atm program
FWelcome()

#username
FUsername()

#password
FPassword()

print("\nHello! Your Username is ", un, " and your password is ",pw )

【问题讨论】:

  • 您需要return unpw 的值才能在您的函数之外使用它们。

标签: python function input


【解决方案1】:

你应该返回值:

def FUsername():
    while True:
        un=input("\nEnter Username ( use only letters ):")
        if un.isalpha():
            break
        else : #invalid
            print ("Invalid Username. Use of symbols and/or numbers are not allowed.\n")
    return un

def FPassword():
    while True:
        pw=input("Enter Password ( use only numbers ):")
        if pw.isnumeric():
            break
        else : #invalid
            print ("Invalid Password. Use of symbols and/or letters are not allowed.\n")
    return pw

#atm program
FWelcome()

#username
un = FUsername()

#password
pw = FPassword()

【讨论】:

  • 返回值是必要的,但还不够;调用函数时还需要将值分配给变量。当然,您已经知道这一点,因为您建议的代码正是这样做的。我只是认为值得在散文中重申:-)
  • 非常感谢,很抱歉找不到我要问的帖子
【解决方案2】:

unFUsername 内部定义,因此无法全局访问。您要么必须将“print un”语句放在函数内,要么使用全局默认虚拟值声明它,然后在 FUsername 中更新它

【讨论】:

    【解决方案3】:

    您在函数内部声明 un,但尝试在该函数外部访问它。解决此问题的一种方法是返回值。另一种方法是使变量成为全局变量。

    #Comment the 2 below lines out if you want to return the variables from the function.
    un=input("\nEnter Username (use only letters ):")
    pw=input("Enter Password ( use only numbers ):")
    def FWelcome():
        print("WELCOME!")
        print("Please Log-In to access ATM:")
        return;
    
    def FUsername():
        while True:
            #Comment this out if you want to declare it globally
            un=input("\nEnter Username ( use only letters ):")
            if un.isalpha():
                break
            else : #invalid
                print ("Invalid Username. Use of symbols and/or numbers are not allowed.\n")
        return un
    
    def FPassword():
        while True:
            #Comment this out if you want to declare it globally.
            pw=input("Enter Password ( use only numbers ):")
            if pw.isnumeric():
                break
            else : #invalid
                print ("Invalid Password. Use of symbols and/or letters are not allowed.\n")
     #Or return the variable un
        return pw
    
    #atm program
    FWelcome()
    
    #username
    FUsername()
    
    #password
    FPassword()
    
    
    
    print("\nHello! Your Username is ", un, " and your password is ",pw )
    

    【讨论】:

    • 仅供参考,return 语句末尾不需要分号
    • 好的,unpw 现在存在于全局范围内,因此 NameError 不会再发生;那挺好的。但是现在程序每次都要求输入用户名和密码两次,并且它忽略了您为每个输入的第二个值,而是坚持使用第一个值,无论它们是否实际上是有效的输入;这很糟糕。
    • 您需要在第二次要求用户输入时注释掉它。抱歉,我以为我在评论中提到了这一点,但我忘记了。我将编辑帖子以反映这一点。
    • @DavidG 谢谢,我想这只是习惯
    • 谢谢大家,我现在学会了如何正确使用函数。祝你有美好的一天
    猜你喜欢
    • 1970-01-01
    • 2015-03-23
    • 1970-01-01
    • 2016-05-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-02
    相关资源
    最近更新 更多