2014-03-19 84 views
0

Python新手,我的代碼正在驗證,除了最後幾行。我需要幫忙計算和計算答案來計算一個百分比,我得到的錯誤。我無法顯示百分比,因爲我無法通過計數功能。這是有錯誤的代碼。我還需要添加一個計時器和一個隨機化問題的方法。Python編程幫助(計算百分比)

打印( 「|總比分:」 + STR(計數)+ 「/ 10」) NameError:名字 '計數' 沒有定義

print("Here is a quiz to test your trivia knowledge...") 
print() 
print() 
print("Question 1") 
print("What month does the New Year begin? a=dec, b=Jan, c=Feb, d=Mar: ") 
print() 

answer = input("Make your choice >>>> ") 
if answer == "a": 
print("Correct!") 
elif answer != "a": 
print ("Wrong") 

print() 
print() 
print("Question 2") 
print("an antipyretic drug reduces what in humans? a=fever, b=weight, c=hair, d=acne: ") 
print() 

print() 
answer = input("Make your choice >>>> ") 
if answer == "a": 
print("Correct!") 
elif answer != "a": 
print ("Wrong") 

print() 
print() 
print("Question 3") 
print("Which artist performed the 1992 hit song titled Save the best For Last? a=Prince, b=U2, c=Vanessa Williams, d=cher: ") 
print() 
answer = input("Make your choice >>>> ") 
if answer == "c": 
print("Correct!") 
elif answer != "c": 
print ("Wrong") 

print() 
print() 
print("Question 4") 
print("Columbus day in the US is celebrated during which month? a=dec, b=Jan, c=Oct, d=Jun: ") 
print() 
answer = input("Make your choice >>>> ") 
if answer == "c": 
print("Correct!") 
elif answer != "c": 
print ("Wrong") 

print() 
print() 
print("Question 5") 
print("What is the largest ocean in the world? a=Indian, b=atlantic, c=Pacific, d=baltic: ") 
print() 
answer = input("Make your choice >>>> ") 
if answer == "c": 
print("Correct!") 
elif answer != "c": 
print ("Wrong") 

print() 
print() 
print("Question 6") 
print("Which European city hosted the 2004 Summer Olympic Games? a=Rome, b=Paris, c=Vienna, d=athens: ") 
print() 
answer = input("Make your choice >>>> ") 
if answer == "d": 
print("Correct!") 
elif answer != "d": 
print ("Wrong") 

print() 
print() 
print("Question 7") 
print("Which NFL team has played in the most Superbowls? a=dallas, b=Pittsburgh,c=atlanta, d=chicago: ") 
print() 
answer = input("Make your choice >>>> ") 
if answer == "a or b": 
print("Correct!") 
elif answer != "a or b": 
print ("Wrong") 

print() 
print() 
print("Question 8") 
print("Which US president is on the $1 bill? a=Washington, b=Lincoln, c=Jefferson, d=Clinton: ") 
print() 
answer = input("Make your choice >>>> ") 
if answer == "a": 
print("Correct!") 
elif answer != "a": 
print ("Wrong") 

print() 
print() 
print("Question 9") 
print("What is the official language of Iran? a=English, b=German, c=Persian, d=Dutch: ") 
print() 
answer = input("Make your choice >>>> ") 
if answer == "c": 
print("Correct!") 
elif answer != "c": 
print ("Wrong") 

print() 
print() 
print("Question 10") 
print("Which flower, herb and perfume ingredient can be found in abundance in Provence, France? a=rose, b=tulip, c=lavender, d=rose: ") 
print() 
answer = input("Make your choice >>>> ") 
if answer == "c": 
print("Correct!") 
elif answer != "c": 
print ("Wrong") 

print ("\n|Congratulations!") 
print ("|Here's your result.") 
print ("|Total score: " + str(count) + "/10") 
division = float(count)/float(20) 
multiply = float(division*100) 
result = round(multiply) 
print ("|Total percentage is", int(result), "%") 

if result >= 95: 
print ("|Grade: a+ \n|Well done!") 

elif result >= 80: 
print ("|Grade: b \n|Good job!") 

elif result >= 65: 
print ("|Grade: c \n|You did okay.") 

elif result >=50: 
print ("|Grade: d \n|Keep trying, that wasn't very good.") 

elif result >= 0: 
print ("|Grade: Fail\n|You need to study.")  
+0

計數變量在哪裏定義?同時請正確格式化代碼,因爲這個標識,它不是一個有效的python代碼 – marcadian

+0

你可能會考慮如何把它放在一個循環中:有一個問題清單和一個正確答案的並行列表,然後循環遍歷它們函數或一組命令來詢問下一個問題並檢查答案。 – beroe

回答

1

NameError通常意味着你試圖訪問尚未定義的變量。如果你檢查你的代碼,你會看到計數出現第一次來這裏

print ("|Total score: " + str(count) + "/10") 

所以實際上沒有什麼要轉換爲字符串呢。也許你想有一個正確的答案櫃檯?你應該這樣做

count = 0 

if answer == "c": 
    print("Correct!") 
    count += 1 
else: 
    print("Wrong") 
+0

欣賞它,先生,謝謝。 – user3435458

+0

如果您認爲問題已解決,請務必將答案標記爲已接受:) – jminuscula