python编程帮助(计算百分比)

时间:2014-03-19 00:01:34

标签: python

Python的新手,我的代码正在验证,除了最后几行。我需要帮助计算和计算答案来计算百分比,我得到错误。我没有显示百分比,因为我无法通过计数功能。这是有错误的代码。我还需要添加计时器和随机化问题的方法。

print(“|总分:”+ 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.")    

1 个答案:

答案 0 :(得分:1)

NameError通常表示您正在尝试访问尚未定义的变量。如果您检查代码,则会在第一时间看到 count

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

所以实际上没有什么可以转换成字符串了。也许你想要一个正确答案的计数器?你应该做点什么

count = 0

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