patternpythonMinor
Simple quiz program
Viewed 0 times
quizsimpleprogram
Problem
I have been trying to make a simple "quiz" program in Python. What I plan to make is, say, a quiz of 3 rounds and each round having 3 questions. And at the end of the every round, the program will prompt the user to go for the "bonus" question or not.
```
print("Mathematics Quiz")
question1 = "Who is president of USA?"
options1 = "a.Myslef\nb. His dad\nc. His mom\nd. Barack Obama\n"
print(question1)
print(options1)
while True:
response = input("Hit 'a', 'b', 'c' or 'd' for your answer\n")
if response == "d":
break
else:
print("Incorrect!!! Try again.")
while True:
response = input("Hit 'a', 'b', 'c' or 'd' for your answer\n")
if response == "d":
stop = True
break
else:
print("Incorrect!!! You ran out of your attempts")
stop = True
break
if stop:
break
# DO the same for the next questions of your round (copy-paste-copy-paste).
# At the end of the round, paste the following code for the bonus question.
# Now the program will ask the user to go for the bonus question or not
while True:
bonus = input("Would you like to give a try to the bonus question?\nHit 'y' for yes and 'n' for no.\n")
if bonus == "y":
print("Who invented Facebook?")
print("a. Me\nb. His dad\nc. Mark Zuckerberg\nd. Aliens")
while True:
response = input("Hit 'a', 'b', 'c' or 'd' for your answer\n")
if response == "c":
break
else:
print("Incorrect!!! Try again.")
while True:
response = input("Hit 'a', 'b', 'c' or 'd' for your answer\n")
if response == "c":
stop = True
break
else:
print("Incorrect!!! You ran out of your attempts")
stop = True
break
if st
```
print("Mathematics Quiz")
question1 = "Who is president of USA?"
options1 = "a.Myslef\nb. His dad\nc. His mom\nd. Barack Obama\n"
print(question1)
print(options1)
while True:
response = input("Hit 'a', 'b', 'c' or 'd' for your answer\n")
if response == "d":
break
else:
print("Incorrect!!! Try again.")
while True:
response = input("Hit 'a', 'b', 'c' or 'd' for your answer\n")
if response == "d":
stop = True
break
else:
print("Incorrect!!! You ran out of your attempts")
stop = True
break
if stop:
break
# DO the same for the next questions of your round (copy-paste-copy-paste).
# At the end of the round, paste the following code for the bonus question.
# Now the program will ask the user to go for the bonus question or not
while True:
bonus = input("Would you like to give a try to the bonus question?\nHit 'y' for yes and 'n' for no.\n")
if bonus == "y":
print("Who invented Facebook?")
print("a. Me\nb. His dad\nc. Mark Zuckerberg\nd. Aliens")
while True:
response = input("Hit 'a', 'b', 'c' or 'd' for your answer\n")
if response == "c":
break
else:
print("Incorrect!!! Try again.")
while True:
response = input("Hit 'a', 'b', 'c' or 'd' for your answer\n")
if response == "c":
stop = True
break
else:
print("Incorrect!!! You ran out of your attempts")
stop = True
break
if st
Solution
import string
NUMBER_OF_ATTEMPTS = 2
ENTER_ANSWER = 'Hit %s for your answer\n'
TRY_AGAIN = 'Incorrect!!! Try again.'
NO_MORE_ATTEMPTS = 'Incorrect!!! You ran out of your attempts'
def question(message, options, correct, attempts=NUMBER_OF_ATTEMPTS):
'''
message - string
options - list
correct - int (Index of list which holds the correct answer)
attempts - int
'''
optionLetters = string.ascii_lowercase[:len(options)]
print message
print ' '.join('%s: %s' % (letter, answer) for letter, answer in zip(optionLetters, options))
while attempts > 0:
response = input(ENTER_ANSWER % ', '.join(optionLetters)) # For python 3
#response = raw_input(ENTER_ANSWER % ', '.join(optionLetters)) # For python 2
if response == optionLetters[correct]:
return True
else:
attempts -= 1
print TRY_AGAIN
print NO_MORE_ATTEMPTS
return False
print("Mathematics Quiz")
# question1 and question2 will be 'True' or 'False'
question1 = question('Who is president of USA?', ['myself', 'His Dad', 'His Mom', 'Barack Obama'], 3)
question2 = question('Who invented Facebook?', ['Me', 'His Dad', 'Mark Zuckerberg', 'Aliens', 'Someone else'], 2)I'm not sure which python you are using. Try both line 20 or line 21 to see which works best for you.
Overall this function allows you to enter in questions with as many responses as you want and it will do the rest for you.
Good luck.
Code Snippets
import string
NUMBER_OF_ATTEMPTS = 2
ENTER_ANSWER = 'Hit %s for your answer\n'
TRY_AGAIN = 'Incorrect!!! Try again.'
NO_MORE_ATTEMPTS = 'Incorrect!!! You ran out of your attempts'
def question(message, options, correct, attempts=NUMBER_OF_ATTEMPTS):
'''
message - string
options - list
correct - int (Index of list which holds the correct answer)
attempts - int
'''
optionLetters = string.ascii_lowercase[:len(options)]
print message
print ' '.join('%s: %s' % (letter, answer) for letter, answer in zip(optionLetters, options))
while attempts > 0:
response = input(ENTER_ANSWER % ', '.join(optionLetters)) # For python 3
#response = raw_input(ENTER_ANSWER % ', '.join(optionLetters)) # For python 2
if response == optionLetters[correct]:
return True
else:
attempts -= 1
print TRY_AGAIN
print NO_MORE_ATTEMPTS
return False
print("Mathematics Quiz")
# question1 and question2 will be 'True' or 'False'
question1 = question('Who is president of USA?', ['myself', 'His Dad', 'His Mom', 'Barack Obama'], 3)
question2 = question('Who invented Facebook?', ['Me', 'His Dad', 'Mark Zuckerberg', 'Aliens', 'Someone else'], 2)Context
StackExchange Code Review Q#22822, answer score: 4
Revisions (0)
No revisions yet.