I have made a General Knowledge quiz in python as a project from my school
import time
score=int()
print("Welcome to the General knowledge Quesions Quiz")
time.sleep(2)
print("Question 1")
time.sleep(2)
Question1 = input ("A vehicle with the national registration code ‘PK’ would originate from which country? ")
if Question1 == ("Pakistan") or Question1 == ("pakistan"):
print("Correct answer")
score = score + 1
else:
print("Incorrect answer, the correct answer is Pakistan")
time.sleep(2)
print("Question 2")
time.sleep(2)
Question2 = input("In which English county is Blenheim Palace? ")
if Question2 == ("Oxfordshire") or Question2 == ("oxfordshire"):
print("Correct answer")
score = score + 1
else:
print("Incorrect answer, the correct answer is Oxfordshire")
time.sleep(2)
print("Question 3")
time.sleep(2)
Question3 = input("How many hours are there in a leap year? ")
if Question3 == ("8784"):
print("Correct answer")
score = score + 1
else:
print("Incorrect answer, the correct answer is 8784")
time.sleep(2)
print("Question 4")
time.sleep(2)
Question4 = input("In which sport could you win the Davis Cup? ")
if Question4 == ("Tennis") or Question4 == ("tennis"):
print("Correct answer")
score = score + 1
else:
print("Incorrect answer, the correct answer is Tennis")
time.sleep(2)
print("Question 5")
time.sleep(2)
Question5 = input("What is the capital of Afghanistan? ")
if Question5 == ("Kabul") or Question5 == ("kabul"):
print("Correct answer")
score = score + 1
else:
print("Incorrect answer, the correct answer is Kabul")
time.sleep(2)
print("Question 6")
time.sleep(2)
Question6 = input("How many golden stars feature on the flag of the European Union? ")
if Question6 == ("twelve") or Question6 == ("Twelve") or Question6 == ("12"):
print("Correct answer")
score = score + 1
else:
print("Incorrect answer, the correct answer is twelve")
time.sleep(2)
print("Question 7")
time.sleep(2)
Question7 = input("In which year did Channel 4 begin transmission in the UK? ")
if Question7 == ("1982"):
print("Correct answer")
score = score + 1
else:
print("Incorrect answer, the correct answer is 1982")
time.sleep(2)
print("Question 8")
time.sleep(2)
Question8=input("In which year did Theresa May become Prime Minister? ")
if Question8 == ("2016"):
print("Correct answer")
score = score + 1
else:
print("Incorrect answer, the correct answer is 2016")
time.sleep(2)
print("Question 9")
time.sleep(2)
Question9 = input("What is six-fourteenths of 112? ")
if Question9 == ("48") or Question9 == ("forty eight"):
print("Correct answer")
score = score + 1
else:
print("Incorrect asnwer, the correct answer is 48")
time.sleep(2)
print("Question 10")
time.sleep(2)
Question10 = input("Which type of angle is greater than 90 degrees but less than 180 degrees? ")
if Question10 == ("obtuse") or ("Obtuse"):
print("Correct answer")
score = score + 1
time.sleep(2)
print("This is the end of the quiz")
time.sleep(1)
print("You got",(score),"/ 10")
num1 = (score)
num2 = 10
print('{0:.2f}%'.format((num1 / num2 * 100)))
I have finished the quiz and if anyone has any suggestions on how I could have improved it, they will be greatly appreciated. So please suggest how I can improve it!
Thanks :)
-
\$\begingroup\$ is this your post by your unregistered account? if so we can request to have the accounts merged \$\endgroup\$Sᴀᴍ Onᴇᴌᴀ– Sᴀᴍ Onᴇᴌᴀ ♦2018年11月29日 19:25:25 +00:00Commented Nov 29, 2018 at 19:25
-
\$\begingroup\$ No it isn't my account \$\endgroup\$colkat406– colkat4062018年11月29日 20:21:53 +00:00Commented Nov 29, 2018 at 20:21
2 Answers 2
You should generally avoid redundancy when programming.
I'd make a list and loop through it
QUESTIONS = [("A vehicle with the national registration code ‘PK’ would originate from which country?", "Pakistan"),
("In which English county is Blenheim Palace?", "Oxfordshire")] # Add more to this list
for n,(question, answer) in enumerate(QUESTIONS, 1):
time.sleep(2)
print("Question", n)
time.sleep(2)
user_answer = input(question)
if user_answer.lower() == answer.lower():
print("Correct answer")
score += 1
else:
print("Incorrect answer, the correct answer is", answer)
And so on
Hope this helps :-)
-
1\$\begingroup\$ Im getting an error with your code so could you possible check over it please and this is the error if user_answer.tolower() == answer.tolower(): AttributeError: 'str' object has no attribute 'tolower' \$\endgroup\$colkat406– colkat4062018年11月28日 16:51:31 +00:00Commented Nov 28, 2018 at 16:51
-
1
score = int()
Don't do this. Just say score = 0
.
Quesions
- run your UI text through spell check.
Why are you calling sleep()
? These calls are not really conducive to a useful user interface. If it's a matter of interacting with the user in a manner that waits to prepare them for the next question, there are better ways to do this - press any key to continue, or if the application calls for it, a countdown timer display. But blindly sleep()
ing is generally not a good idea.
score = score + 1
Don't do this. Just do score += 1
In two places you write (score)
. These parens don't do anything and should be dropped. Similarly, there's no need for double-parens around ((num1 / num2 * 100))
.