0

我已经开始在学校学习 Python 大约一个月了,我决定做一个测验。我已经添加了一个评分系统,因此如果您错误地回答了一个问题,它会告诉您您的分数。但是,这是行不通的,它总是会给你一个 0 分。另外,如果他们没有通过一个问题,有没有办法只写一个 else 语句,而不是每个问题一个?谢谢 :)

这是代码示例(Python 3.2.3):

#QUIZ


print("Welcome to the quiz")
print("Please choose a difficulty:")
difficulty = input("A) Hard   B)Easy")


if difficulty == "A":
    score = 0
    print("")
def question(score):
     print("You chose the hard difficulty")
     print("Where is the Great Victoria lake located?")
     answer1 = input("A) Canada   B)West Africa   C)Australia   D)North America")
     if answer1 == "C":
         print("Correct")
         score = score+1
     else:
         print("you failed the quiz")
         print("Score:",score)
         quit()





def question2(score):
     print("Who is most responsible for cracking the Enigma Code")
     answer2 = input("A) Alan Turing   B) Jeff Bezos   C) George Boole   D) Charles   Babbage")
     if answer2 == "A":
         print("Correct")
         score = score+1
     else: 
         print("you failed the quiz")
         print("Score:",score)
         quit()




def diff_easy(difficulty):
    if difficulty == "B":
        score2 = 0
        print("")


def question4(score2):
        print("You chose the easy difficulty")
        print("What is the capital of Australia?")
        answer1 = input("A) Canberra   B) Sydney   C)Melbourne")
        if answer1 == "A":
            print("Correct")
            score2 = score2+1
 else:
         print("you failed the quiz")
         print("Score:",score2)
         quit()


def question5(score2):
    print("When was the Great Fire of London?")
    answer2 = input("A) 1666   B) 1555   C)1605")
    if answer2 == "A":
         print("Correct")
         score2 = score2+1

    else:
         print("you failed the quiz")
         print("Score:",score2)
         quit()

if difficulty == "A":
    question(score)
    question2(score)



if difficulty == "B":
    diff_easy(difficulty)
    question4(score2)
    question5(score2)
4

2 回答 2

3

这是因为score您在函数中看到的变量是您设置的变量的副本,按值score传递(我强烈建议您修改此主题)。您需要一种传递状态的方法。

很快你就会发现对象,现在(以后再也不会!),一个简单的解决方案是让score变量成为全局变量。只需更换

def question2(score):

def question2():

在您的所有问题函数中并添加global score为每个问题的第一个语句,如下所示:

def question5():
    global score
    print("When was the Great Fire of London?")
    answer2 = input("A) 1666   B) 1555   C)1605")
    if answer2 == "A":
         print("Correct")
         score = score + 1 
    else:
         print("you failed the quiz")
         print("Score:", score)
         quit()

替换所有出现的score2withscore就完成了。

当然,您可以if: else对所有问题使用单个分支。我不会给你完整的解决方案,这样你就可以锻炼了,但这里有一个提示:创建一个接受三个参数的函数:

  1. 问题
  2. 可能的答案列表
  3. 正确答案

让我们调用这个函数quiz。现在你可以像这样使用它:

quiz("When was the Great Fire of London?", ["1666", "1555", "1605"], "1666")
quiz("What is the capital of Australia?", ["Canberra", "Sydney", "Melbourne"], "Canberra")
于 2013-10-06T18:29:09.043 回答
0

The statements like

   score2 = score2+1

in your code have no effect, because they modify a local variable.

For the other question: you should use a data structure to store questions and answers, so that you can iterate over them without repeating the same code many times.

于 2013-10-06T18:31:20.307 回答