我现在已经更改了我的代码但是它说最后一行的语法无效,我该如何解决这个问题?我在displayMenu之前将最后一行更改为全局状态,但它似乎导致我的代码出现问题
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 use
答案 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()