Python测验代码改进,也许有一个弹出窗口?

时间:2017-07-01 16:27:40

标签: python python-3.x

我是新手,如果我不小心违反任何规则,我很抱歉。基本上我是Python的初学者,并为TV Show Trivia上的测验制作了一段代码。我想在这里向社区询问是否有任何方法可以整理它,例如不必要的重复等。另外,我想知道是否有办法使测试出现在弹出窗口中,空间来输入答案。这是我到目前为止的代码:

print("Shall we start?")
input("Press Enter to continue...")
score = 0

Que_1 = input("Who's missing - Rachel, Ross, Chandler, Pheobe?: ")
if Que_1 == "Monica" or "monica":
    print("That's right, well done!!")
    score = score + 1
else:
    print("I'm sorry, that's the wrong answer :( ")
    score = score - 1

Que_2 = input("What is the first name of the women Leonard from The Big Bang 
Theory ended up marrying?: ")
if Que_2 == "Penny" or "penny":
    print("Congratulations, that was correct!!")
    score = score + 1
else:
    print("Incorrect")
    score = score - 1

Que_3 = input("What is the full name of the actor who plays the 10th 
Doctor?: ")
if Que_3 == "David Tennant" or "david tennant":
    print("Oui")
    score = score + 1
else:
    ("Non")
    score = score - 1

Que_4 = input("Which sister died in Charmed?: ")
if Que_4 == "Prue" or "prue":
    print("You know it")
    score = score + 1
else:
    print("You don't know it")
    score = score - 1

Que_5 = input("Who is the main character of 13 reasons why(Full Name)?: ")
if Que_5 == "Hannah Baker" or "hannah baker":
    print("Correctumondo")
    score = score + 1
else:
    print("Failumundo")
    score = score - 1

Que_6 = input("Complete the name 'Tracy ______': ")
if Que_6 == "Beaker" or "beaker":
    print("Perfect")
    score = score + 1
else:
    print("Not perfect")
    score = score - 1

Que_7 = input("Lily, Jas and ______ : ")
if Que_7 == "Martha" or "martha":
    print("On the button")
    score = score + 1
else:
    print("Sucks to be you")
    score = score - 1

Que_8 = input("Is Missy from Ackley Bridge straight, lesbian or bisexual?:")
if Que_8 == "Straight" or "straight":
    print("Right on")
    score = score + 1
else:
    print("Way off")
    score = score - 1

Que_9 = input("From the 4 o' Clock Club, who is Josh's brother?: ")
if Que_9 == "Nathan" or "nathan":
    print("Unmistaken")
    score = score + 1
else:
    print("Mistaken")
    score = score - 1

Que_10 = input("What does Sophie from My Parents Are Aliens have to eat in 
order to shapeshift? : ")
if Que_10 == "Ice cream" or "Ice Cream" or "ice cream":
    print("Amen to that")
    score = score + 1
else:
    print("Ughhhh just no")
    score = score - 1

if score == 10:
    print("10 out of 10. Your a boss at this")

elif 5 <= score < 10:
    print("Your score is", score, "pretty good, try to get 10 next time")

elif 1 <= score < 10:
    print("Your score is", score, "Better luck next time")

3 个答案:

答案 0 :(得分:1)

试试这段代码(我没有提到所有问题):

questions = [
    "Who's missing - Rachel, Ross, Chandler, Pheobe?: ",
    "What is the first name of the women Leonard from The Big Bang Theory ended up marrying?: ",
    "What is the full name of the actor who plays the 10th Doctor?: ",
    "Which sister died in Charmed?: "
]

answers = [
    "monica",
    "penny",
    "david tennant",
    "prue"
]

yes_nos = [
    ["That's right, well done!!", "I'm sorry, that's the wrong answer :( "],
    ["Congratulations, that was correct!!", "Incorrect"],
    ["Oui", "Non"],
    ["You know it", "You don't know it"]
]

if __name__ == '__main__':
    print("Shall we start?")
    input("Press Enter to continue...")
    score = 0

    for i, q in enumerate(questions):
        ans = input(q + '\n').lower()
        if ans == answers[i]:
            print(yes_nos[i][0])
            score += 1
        else:
            print(yes_nos[i][1])
        print()

    if score == 10:
        print("10 out of 10. Your a boss at this")
    elif 5 <= score < 10:
        print("Your score is", score, "\nPretty good, try to get 10 next time")
    elif 1 <= score < 10:
        print("Your score is", score, "\nBetter luck next time")

答案 1 :(得分:0)

Easygui非常适合小对话框:

name=Ramesh
age=20
uid=15762gfyf
EOM

name=Tom
age=20
uid=15762gfyd
EOM

name=Gavin
age=25
uid=15762gfyz
EOM

会弹出像this这样的窗口。

希望这有帮助!

答案 2 :(得分:0)

在优化方面,您可以使用方法lower()

if Que_1.lower() ==  "monica":

这将删除第一个字母作为大写

您也可以使用+ = 1

print("That's right, well done!!")
score += 1

您无需执行score = score + 1

在第一个Que_1上,您需要在else之后设置得分等于0,因为这是您的第一个。

您可能需要检查词典以及它是否可以帮助您。

最后,您可以使用此question来了解您可以做些什么。