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!')
解决方案
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