2014-12-06 65 views
-1

我有這樣的代碼:如何在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新手)。

我該怎麼做?

+0

考慮使用「其他」,而不是多個'IFS ... wrong' – 2014-12-06 23:07:03

回答

0

您是部分與有辦法:

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

但是,您需要將新的值賦給score

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

而且你需要與啓動功能:

score = 0 
+0

或'得分+ = 1' ...... – 2014-12-06 22:38:15

+0

我還要通過把主張一個數據驅動結構中的所有問題 - 選項 - 回答列表中的元組並循環。但這個問題沒有要求。 – 2014-12-06 22:46:38

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. 爲了得到頭文件「Question x」,我使用了enumerate,它需要iterable作爲參數,我已經使用了字典問題,它將迭代它的關鍵字(其中的問題),以及返回兩個變量,question_numberquestion
  4. 枚舉器從索引0開始,因此我們在其中添加1以將第一個問題顯示爲「問題1」而不是「問題0」
  5. 我們遍歷問題的值,語法是dictionary [key ]選項,因爲我們不想顯示我們使用[-1]去除最後一項
  6. 的答案,現在我們檢查答案是否正確,如果user_choice等於值中的最後一項dicionary(請記住[-1]指的是最後一項)。
  7. 如果答案是正確的,我們遞增1
  8. 比分否則我們只是打印「錯誤」
  9. 後所有問題都顯示我們打印的分數。
+0

Imho一個更好的數據結構是一個元組列表,'[(q1,(a11,a12,a13,a14),ans1),...]'使程序可以按給定的順序顯示問題,以反映問卷的不變性。這是一個很好的答案,強調抽象的有用性。 – gboffi 2014-12-06 23:39:46

+0

事實上,元組很好,但他們缺少一個小細節,他們在訪問它們時顯得很奇怪,並且向新人展示了一個嵌套的具有多個深度級別的元組列表,這些元素的負擔過重。字典對新用戶更友好。 – 2014-12-06 23:45:09

+0

爲了避免無序的問題,我們仍然可以使用OrderedDict – 2014-12-06 23:52:06