簡體   English   中英

python編程幫助(計算百分比)

[英]python programming help (calculate percentage)

Python的新功能,除了最后幾行,我的代碼正在驗證中。 我需要統計和計算答案以計算百分比的幫助,但我遇到了錯誤。 我沒有顯示百分比,因為我無法通過count函數。 這是有錯誤的代碼。 我還需要添加計時器和隨機分配問題的方法。

打印(“ |總分:” + str(count)+“ / 10”)NameError:未定義名稱“ count”

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.")    

通常,NameError表示您正在嘗試訪問尚未定義的變量。 如果您檢查您的代碼,您會看到該計數首次出現在此處

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

因此實際上還沒有任何東西可以轉換為字符串。 也許您想找一個櫃台來獲得正確答案? 你應該做類似的事情

count = 0

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

暫無
暫無

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

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