如何在 Python 中制作分数计数器?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27337331/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 01:41:16  来源:igfitidea点击:

How do I make a score counter in Python?

pythonpython-3.x

提问by R231

I have this code:

我有这个代码:

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)

I would like to insert a score counter which would add a point every time the user gets one right and does nothing when they get one wrong. I would like it to be very simple and easy to write (I am new to Python).

我想插入一个分数计数器,它会在用户每次做对时增加一分,而当他们做错时什么都不做。我希望它非常简单且易于编写(我是 Python 新手)。

How would I do this?

我该怎么做?

回答by Dan D.

You are part way there with:

你在那里的一部分:

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

However, you need to assign the new value to score:

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

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

And you need to start your function with:

你需要开始你的功能:

score = 0

回答by f.rodrigues

I know this is not part of the question, but conside using a dictionary or list to store the questions, options and answer and just loop over:

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

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

Explanation:

解释:

  1. questionis a python dictionary, it has a keyand a value, the key in this case is the question, the value is a list, in this list we have all the possible options, and in the last item the answer;
  2. Here's the score, note that it is outside the for loop, because we wan't to maintain it over all the question, just increment it if correct.
  3. in order to get the header "Question x", I've used a enumerate, it takes a iterableas argument, I've used the dictionary question, it will iterate over it's keys(the questions in it), and returns two variables, the question_numberand the question.
  4. enumerator starts in the index 0, so we add 1 to it to display the first question as "Question 1" instead of "Question 0"
  5. we loop over the values of the question, the syntax is dictionary[key], the options, since we don't want to show the answer we use [-1] to remove the last item
  6. now we check if the answer is correct, if the user_choice is equal to the last item in the values of the dicionary(remember the [-1] refers to the last item).
  7. if the answer is correct we increment the score by 1
  8. else we just print "wrong"
  9. after all the questions are displayed we print the score.
  1. question是一个python字典,它有akey和a value,在这种情况下,关键是问题,值是a list,在这个列表中我们有所有可能的选项,最后一项是答案;
  2. 这是score,请注意它在 之外for loop,因为我们不想在所有问题上都保持它,如果正确,只需增加它。
  3. 为了获得标题“问题 x”,我使用了 a enumerate,它将 aiterable作为参数,我使用了字典问题,它将迭代它的键(其中的问题),并返回两个变量,question_numberquestion
  4. 枚举数从索引 0 开始,因此我们向其添加 1 以将第一个问题显示为“问题 1”而不是“问题 0”
  5. 我们遍历问题的值,语法是字典 [key],选项,因为我们不想显示答案,所以我们使用 [-1] 删除最后一项
  6. 现在我们检查答案是否正确,如果 user_choice 等于字典值中的最后一项(记住 [-1] 指的是最后一项)。
  7. 如果答案正确,我们将分数加 1
  8. 否则我们只是打印“错误”
  9. 显示所有问题后,我们打印分数。