2017-10-09 41 views
0

我現在已經更改了我的代碼,但是在那裏說最後一行的語法無效,我該如何解決這個問題?我改變了最後一行的全球地位displayMenu之前,但它似乎是引起問題,以我的代碼當我使用input()時,我的代碼在我運行時沒有出現

users = {} 
status = "" 

def displayMenu(): 
    status = raw_input("Are you a registered user? y/n? Press q to quit: ") 
    if status == "y": 
     oldUser() 
    elif status == "n": 
     newUser() 

def newUser(): 
    createLogin =("Create login name: ") 

    if createLogin in users: # check if login name exists 
     print ("\nLogin name already exist!\n") 
    else: 
     createPassw =("Create password: ") 
     users[createLogin] = createPassw # add login and password 
     print("\nUser created!\n")  

def oldUser(): 
    login = raw_input("Enter login name: ") 
    passw = raw_input("Enter password: ") 

    # check if user exists and login matches password 
    if login in users and users[login] == passw: 
     print ("\nLogin successful!\n") 
    else: 
     print ("\nUser doesn't exist or password error!\n") 

    if passw == users[login]: 
     print ("Login successful!\n") 

while status != "q":    
    global status displayMenu(): 
+2

你使用Python 3還是Python 2?如果你仍然在使用Python 2,你應該使用'raw_input',Python 2'input'函數是危險的。在Python 3中,舊的'input'函數不再存在,'raw_input'已被重命名爲'input'。 –

+0

它看起來像你使用Python 2,所以你應該認真考慮升級到Python 3.同時,改變'status = input()(「你是註冊用戶嗎?y/n?按q退出:」 )'回到'status = raw_input(「你是註冊用戶嗎?y/n?按q退出:」)'。你需要以同樣的方式修復那些'input()'行。 –

+0

謝謝我現在發佈了一個編輯,因爲它似乎有另一個問題 –

回答

0

您有與該代碼的幾個問題。例如,

createLogin =("Create login name: ") 

只是將字符串"Create login name: "的名稱createLogin。但我懷疑,你想"Create login name: "爲用戶提示,所以你需要:

createLogin = raw_input("Create login name: ") 

腳本的最後一行並沒有太大的意義。我懷疑這是試圖使用global指令,但最好避免這種情況,除非你真的需要。而且一般你不需要真的需要它。 :)

這是您的代碼的修復版本。請注意,我們沒有將users字典保存到磁盤,因此當程序退出時,輸入的任何信息都將丟失。

from __future__ import print_function 

users = {} 

def displayMenu(): 
    status = raw_input("Are you a registered user? y/n? Press q to quit: ") 
    if status == "y": 
     oldUser() 
    elif status == "n": 
     newUser() 
    return status 

def newUser(): 
    createLogin = raw_input("Create login name: ") 
    if createLogin in users: # check if login name exists 
     print ("\nLogin name already exist!\n") 
    else: 
     createPassw = raw_input("Create password: ") 
     users[createLogin] = createPassw # add login and password 
     print("\nUser created!\n") 

def oldUser(): 
    login = raw_input("Enter login name: ") 
    passw = raw_input("Enter password: ") 

    # check if user exists and login matches password 
    if login in users and users[login] == passw: 
     print("\nLogin successful!\n") 
    else: 
     print("\nUser doesn't exist or password error!\n") 

status = "" 
while status != "q": 
    status = displayMenu() 
+0

非常感謝你!你是一個拯救生命的人:)我真的應該更多地使用這個網站 –

相關問題