簡體   English   中英

如何在Python中建立一個計分器?

[英]How do I make a score counter in Python?

我有以下代碼:

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的新手)。

我該怎么做?

我知道這不是問題的一部分,而是考慮使用字典或列表來存儲問題,選項和答案,然后循環遍歷:

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. question是一個python字典 ,它有一個key和一個value ,在這種情況下,鍵是問題,值是一個list ,在此列表中,我們有所有可能的選項,最后一個是答案;
  2. 這是score ,請注意它在for loop之外,因為我們不希望在所有問題上都將其保持不變,如果正確就可以將其遞增。
  3. 為了獲得標題“ Question x”,我使用了一個enumerate ,它使用了一個iterable作為參數,我使用了字典問題,它將遍歷它的鍵(其中的問題),並返回兩個變量, question_numberquestion
  4. 枚舉數從索引0開始,因此我們向其添加1以將第一個問題顯示為“問題1”而不是“問題0”
  5. 我們遍歷問題的值,語法是dictionary [key],選項,因為我們不想顯示答案,因此我們使用[-1]來刪除最后一項
  6. 現在我們檢查答案是否正確,如果user_choice等於字典值中的最后一項(記住[-1]指的是最后一項)。
  7. 如果答案是正確的,我們將分數加1
  8. 否則我們只打印“錯誤”
  9. 在顯示所有問題后,我們將打印分數。

您在其中:

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

但是,您需要將新值分配給score

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

您需要使用以下命令啟動功能:

score = 0

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM