如何在Python中创建分数计数器?

时间:2014-12-06 22:30:15

标签: python python-3.x

我有这段代码:

def quiz():

    print("Here is a quiz to test your knowledge!")
    print()
    print("Question 1")
    print("How tall is the Eiffel Tower?")
    print("a. 350m")
    print("b. 342m")
    print("c. 324m")
    print("d. 1000ft")
    answer = input("Make your choice : ")
    if answer == "c" :
        print ("Correct!")
    if answer == "a" :
        print ("Wrong!")
    if answer == "b" :
        print ("Wrong!")
    if answer == "d" :
        print ("Wrong!")
    print()
    print("Question 2")
    print("How loud is a sonic boom?")
    print("a. 160dB")
    print("b. 175dB")
    print("c. 157dB")
    print("d. 213dB")
    answer = input("Make your choice : ")
    if answer == "d" :
        print ("Correct!")

    if answer == "a" :
        print ("Wrong!")
    if answer == "b" :
        print ("Wrong!")
    if answer == "c" :
        print ("Wrong!")
    print()
    print("Question 3")
    print("How hot is Pluto?")
    print("a. 223⁰C to -233⁰C")
    print("b. -323⁰C to -347⁰C")
    print("c. -375⁰F to -395⁰F")
    print("d. -213⁰C to -237⁰C")
    answer = input("Make your choice : ")
    if answer == "c" :
        print ("Correct!")
        score + 1
    if answer == "a" :
        print ("Wrong!")
    if answer == "b" :
        print ("Wrong!")
    if answer == "d" :
        print ("Wrong!")
    print()
    print("Question 4")
    print("How many calories in a normal Twix bar?")
    print("a. 284")
    print("b. 297")
    print("c. 314")
    print("d. 329")
    answer = input("Make your choice : ")
    if answer == "a" :
        print ("Correct!")
        score + 1
    if answer == "c" :
        print ("Wrong!")
    if answer == "b" :
        print ("Wrong!")
    if answer == "d" :
        print ("Wrong!")
    print()
    print("Question 5")
    print("How deep is Mariana Trench?")
    print("a. 12.9km")
    print("b. 11.7km")
    print("c. 12.4km")
    print("d. 11.0km")
    answer = input("Make your choice : ")
    if answer == "d" :
        print ("Correct!")
        score + 1
    if answer == "a" :
        print ("Wrong!")
    if answer == "b" :
        print ("Wrong!")
    if answer == "c" :
        print ("Wrong!")
    print()
    print("Question 6")
    print("How many states are there in the USA?")
    print("a. 50")
    print("b. 59")
    print("c. 65")
    print("d. 48")
    answer = input("Make your choice : ")
    if answer == "a" :
        print ("Correct!")
        score + 1
    if answer == "c" :
        print ("Wrong!")
    if answer == "b" :
        print ("Wrong!")
    if answer == "d" :
        print ("Wrong!")
    print()
    print("Question 7")
    print("How many balls on a snooker table?")
    print("a. 25")
    print("b. 22")
    print("c. 21")
    print("d. 19")
    answer = input("Make your choice : ")
    if answer == "b" :
        print ("Correct!")
        score + 1
    if answer == "a" :
        print ("Wrong!")
    if answer == "c" :
        print ("Wrong!")
    if answer == "d" :
        print ("Wrong!")
    print(score)

我想插入一个分数计数器,每当用户获得一个权利时,它会添加一个点,当他们出错时不会做任何事情。我希望它非常简单易写(我是Python的新手)。

我该怎么做?

2 个答案:

答案 0 :(得分:2)

我知道这不是问题的一部分,但考虑使用字典或列表存储问题,选项和答案,然后循环:

questions = {
    "How tall is the Eiffel Tower?":['a. 350m', 'b. 342m', 'c. 324m', 'd. 1000ft','a'],
    "How loud is a sonic boom?":['a. 160dB', 'b. 175dB', 'c. 157dB', 'd. 213dB', 'd']
} # 1

score = 0 # 2 
for question_number,question in enumerate(questions): # 3
    print ("Question",question_number+1) # 4
    print (question)
    for options in questions[question][:-1]: # 5
        print (options)
    user_choice = input("Make your choice : ")
    if user_choice == questions[question][-1]: # 6
        print ("Correct!")
        score += 1 #7 here's the relevant part of the question
    else: # 8
        print ("Wrong!")

print(score) #9

说明:

  1. questionpython dictionary,它有keyvalue,在这种情况下,关键是问题,值是list ,在此列表中,我们有所有可能的选项,在最后一项中有答案;
  2. 这里是score,请注意它在for loop之外,因为我们不能在所有问题上保持它,只要正确就增加它。
  3. 为了获得标题"问题x",我使用了enumerate,它需要iterable作为参数,我已经使用了字典问题,它将迭代它的键(其中的问题),并返回两个变量,question_numberquestion
  4. 枚举器在索引0中开始,因此我们向它添加1以将第一个问题显示为"问题1"而不是"问题0"
  5. 我们循环问题的值,语法是字典[key],选项,因为我们不想显示我们使用[-1]删除最后一项的答案
  6. 现在我们检查答案是否正确,如果user_choice等于dicionary值中的最后一项(请记住[-1]指的是最后一项)。
  7. 如果答案是正确的,我们将分数增加1
  8. 否则我们只是打印"错误"
  9. 显示所有问题后,我们打印得分。

答案 1 :(得分:0)

你可以与之合作:

if answer == "a" :
    print ("Correct!")
    score + 1

但是,您需要将新值分配给score

if answer == "a" :
    print ("Correct!")
    score = score + 1

你需要通过以下方式启动你的功能:

score = 0