Click here to Skip to main content
15,885,668 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I'm trying to figure out how to make a program run over and over and over again, but I don't know. Could someone help? Thanks! :/ Here is the code:
print('To select an answer, type the number before the answer! Enjoy! Please give us some feedback on how to improve!')

person = input('Enter your name: ')
print'Hello', person

import sys
import random

class Question(object):
def __init__(self, question, answer, options):
self.question = question
self.answer = answer
self.options = options

def ask(self):
print self.question + "?"
for n, option in enumerate(self.options):
print "%d) %s" % (n + 1, option)

response = int(sys.stdin.readline().strip()) # answers are integers
if response == self.answer:
print "CORRECT"
else:
print "WRONG, TRY AGAIN"

questions = [
Question("How many legs are on a horse ", 4, ["one", "two", "three", "four", "five"]),
Question("How many wheels are on a bicycle", 2, ["one", "two", "three","twenty-six"]),
Question("How many letters are in the alphabet", 3, ["twenty four", "twenty two", "twenty six", "twenty eight"]),
Question("Who won the Super Bowl this year", 2,["Seahawks","Patriots", "Packers", "Ravens"]),
Question("Who was the Offensive Rookie of the Year for the National Football League", 1, ["Odell Beckham Junior", "Mike Evans", "Jeremy Hill"]),
Question("Who was the first president of the United States", 2, ["John Adams", "George Washington", "James Madison"]),
Question("How many yards are in a National Football League stadium", 3, ["ninety", "eighty", "hundred"]),
# more verbose formatting
Question(question="What colour is a swan in Australia",
answer=1,
options=["black", "white", "pink", "green"]), # the last one can have a comma, too
]

random.shuffle(questions) # randomizes the order of the questions

for question in questions:
question.ask()
print('Thank you for playing! Want to play again? Simply run this program again!')
Posted
Updated 22-Feb-15 14:03pm
v2
Comments
Kuthuparakkal 22-Feb-15 19:43pm    
Try something like this: https://code.djangoproject.com/wiki/Tutorials
Sergey Alexandrovich Kryukov 22-Feb-15 20:05pm    
What have you tried so far?
One little advice: Remove the words with "enjoy". People are not likely enjoy such things.
—SA

1 solution

Isn't it obvious that with elementary issues reading original documentation is efficient, but asking such question is not?
Please see:
https://docs.python.org/2/tutorial/introduction.html[^],
https://docs.python.org/2/tutorial/controlflow.html[^].

That's just the topic in introduction. Before writing any code, you have to read the whole manual and at least scan through the reference, at least to know where you can find every detail:
https://docs.python.org/2/reference/index.html[^].

In particular, as related to your "problem";
https://docs.python.org/2/reference/compound_stmts.html[^],
https://docs.python.org/2/reference/compound_stmts.html#the-while-statement[^],
https://docs.python.org/2/reference/compound_stmts.html#the-for-statement[^].

And so on…

—SA
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900