Reputation: 3
new to Python, my code is validating except for the last few lines. I need help with counting and calculating the answers to calculate a percentage and I'm getting errors. I haven't gotten to show the percentage because I can't get past the count function. Here is the code with errors. I also need to add a timer and a way to randomize the questions.
print ("|Total score: " + str(count) + "/10") NameError: name 'count' is not defined
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.")
Upvotes: 0
Views: 2791
Reputation: 552
NameError generally means that you are trying to access a variable that has not been defined. If you check your code, you'll see that count appears for the first time here
print ("|Total score: " + str(count) + "/10")
so there's actually nothing to convert to string yet. Maybe you wanted to have a counter for correct answers? You should do something like
count = 0
if answer == "c":
print("Correct!")
count += 1
else:
print("Wrong")
Upvotes: 1