0

私の問題は、このスクリプトでグローバル名が定義されていないというエラーが発生し続けることです。

import time
def cls():
    print(("\n")*100)
#Registers the Username and Password for the user.
def registerPro():
    cls()
    username=str(input("Enter your Username here: "))
    password=str(input("Enter your Password here: "))
    confirm=str(input("Please confirm your Password: "))
    if password==confirm:
        cls()
        print("Thank you for registering at Apex Industries!")
        time.sleep(3)
        loggingIn()
    else:
        cls()
        print("Your passwords do not match, please re-register.")
        time.sleep(3)
        registerPro()
    return username, password
#Is called by getUsername. If the user entered the password correctly then the login is successful.
def getPass(password):
    cls()
    confirmPass=str(input("Enter your password: "))
    if confirmPass==password:
        cls()
        print("You have logged in successfully.")
    else:
        cls()
        print("Wrong Password. Try again")
        getPass()
#Makes sure the username is in the database.
def getUsername(username):
    cls()
    confirmUser=str(input("Enter your username: "))
    if confirmUser==username:
        getPass(password)
    else:
        cls()
        print("No username with that name in the database.")
        time.sleep(3)
        loggingIn()
#The login for the main program.
def loggingIn():
    cls()
    print("Hello and welcome to the Apex Industries login/register page.")
    print("      Please choose to either Login or Register.")
    print()
    regLog=str(input("Type Login or Register for your choice: "))
    if regLog=='Login':
        getUsername(username)
    elif regLog=='Register':
        registerPro()
    else:
        cls()
        print("That is not a valid option. Try again.")
        time.sleep(3)
        loggingIn()

loggingIn()

私のトレースバックは私にこれを与えます:

Traceback (most recent call last):
  File "C:/Users/Evu/Desktop/Python/testforLogin", line 59, in <module>
    loggingIn()
  File "C:/Users/Evu/Desktop/Python/testforLogin", line 13, in loggingIn
    registerPro()
  File "C:/Users/Evu/Desktop/Python/testforLogin", line 29, in registerPro
    loggingIn()
  File "C:/Users/Evu/Desktop/Python/testforLogin", line 11, in loggingIn
    getUsername(username)
NameError: global name 'username' is not defined

私は何を間違っていますか?作成した getUsername() 関数にユーザー名をインポートしたい。私はスクリプトを書くのが初めてで、自分の時間でこれを学ぼうとしているだけであることを覚えておいてください。これは単なるテスト スクリプトです。どんなアドバイスも素晴らしいでしょう。

4

2 に答える 2

0

loggingIn()電話をかけるときにユーザー名を初期化していませんgetUsername(username)- どのユーザー名を検証しようとしていますか? どこから来たのですか? ローカルが見つからない場合username、グローバル名前空間を調べてそこで見つけることができないため、エラーが発生します。

于 2013-09-18T23:57:55.743 に答える