登录开始测验时

时间:2017-11-18 21:02:29

标签: python

我想要一些帮助,因为我很困惑,也真的坚持这个。我想指出我在python上比较大,所以请放轻松。

我正在尝试使用登录/注册字典系统创建一个简单的测验。我也试图将所有“注册用户”保存在.txt文件中,但不幸的是我不能这样做,所以如果有人可以提供帮助,我会很高兴!

问题是,当用户登录或注册时我想要运行测验位,而是登录/注册在循环中运行,除非我拿走while语句,但是如果我把它拿走了登录/注册脚本根本不运行。所以我需要的是......

  

当我的程序运行时,我希望它运行登录/注册脚本但是   然后,当用户注册或登录时,我希望它启动   测验。

这是我的代码,

 #Login & Register
users = {}
status = ""


def displayMenu():
    status = input("Are you a registered user? y/n? Say quit to exit: ")  
    if status == "y":
        oldUser()
    elif status == "n":
        newUser()

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

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

def oldUser():
    login = input("Enter login name: ")
    passw = 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 wrong password!\n")

while status != "q":            
    displayMenu()

# Defining Score variables 
x = 0
score = x

# Question One 
print("When did WW2 finish?")
answer1 = input("a)2001\nb)1945\nc)1877\nd)1940\n:")
if answer1.lower() == "b" or answer1.lower() == "2":
    print("Correct")
    x = x + 1   
else:
    print("Incorrect, the second Worl War ended in 1945")

# Question Two
print("Who was responsilbe of most deaths in World War 1 & 2 ")
answer2 = input("a)Donald Trump\nb)Adolf Hitler\nc)Tom Cruisend\nd)There were no WAR\n:")
if answer2.lower() == "b" or answer1.lower() == "Adolf Hitler":
    print("Correct")
    x = x + 1
else:
    print("Incorrect, It was Adolf Hitler who took around 12 to 14 million lives")

# Question Three
print("True or False... Hitler was a painter")
answer3 = input(":")
if answer3.lower() == "true" or answer3.lower() == "t":
    print("Correct")
    x = x + 1
else:
    print("Incorrect")  

# Question Four
print("What happened in Chernobyl")
answer4 = input("a)Nuclear Plant exploaded\nb)Water Flood\nc)Alien Envasion\nd)War\n:")
if answer4.lower() == "a" or answer4 == "1967":
    print("Correct")
    x = x + 1
else:
    print("Incorrect, the nuclear plant exploaded")

# Question Five 
print("True or False... Everybody knew the reactor will explode")
answer5 = input(":")
if answer5.lower() == "false" or answer5.lower() == "f":
    print("Correct")
    x = x + 1
else:
    print("Incorrect, no one knew it will explode")


#Total Score
score = float(x / 5) * 100
print(x,"out of 5, that is",score, "%")

2 个答案:

答案 0 :(得分:2)

在while循环结束时添加break语句:

while status != "q":            
    displayMenu()
    break

答案 1 :(得分:1)

您的状态不存在于DisplayMenu()函数

范围之外

您可以使用status作为此函数的返回值,添加

return status

最后,然后使用条件:

dm=""
while dm != "q":            
    dm=displayMenu()