-4

I would like to know how I could allow my code to let the user restart the quiz at any time throughout the program however, I have no idea how to do this from scratch. It preferably needs to be fairly simple. Would it be easiest doing it as an if statement and if so what would I put in it? I tried this:

while True:
# main program
while True:
    answer = raw_input('Run again? (y/n): ')
    if answer in ('y', 'n'):
        break
    print 'Invalid input.'
if answer == 'y':
    continue
else:
    print 'Goodbye'
    break
Tom E
  • 11
  • 2
  • I needed it simple however this is one of my first programing attempts. I tried to use a 'while' loop however it didn't seem to work so turned to the forums for advice! – Tom E Oct 20 '17 at 16:37
  • edit your question to include your code... dont post it in the comments – Joran Beasley Oct 20 '17 at 16:41
  • Apoligies, new to this! Done it now – Tom E Oct 20 '17 at 16:42
  • There's some great info at https://stackoverflow.com/questions/23294658/asking-the-user-for-input-until-they-give-a-valid-response BTW, you should seriously consider learning Python 3, Python 2 will reach its official End Of Life in 2020. – PM 2Ring Oct 20 '17 at 16:46
  • @TomE Making something simple for the *user* is often very complicated for the *programmer*. There's no free lunch. The easiest way is probably to represent your entire quiz as a data structure (e.g a list of questions), and then use something about that structure to keep track of where you are (e.g an index into that list). Then, you'll have to save that list to a file somewhere, so you can re-read it when the user opens your program again. – jpaugh Oct 20 '17 at 18:06
  • While that is a very flexible approach, the downside is that your program will need to read and understand the data structure (i.e. symbolic manipulation), rather than simply encoding the quiz directly into the program logic (i.e. code). It's a higher level of abstraction, with more flexibility, and which is more difficult to understand. – jpaugh Oct 20 '17 at 18:08

1 Answers1

1

breaking your problem down into parts is the first step

# 1. ask a question and validate the response
def ask_question(prompt,possible_choices):
    while True:  
       result = raw_input(prompt)
       if result in possible_choices:
          return result
       print "Invalid Response Please Enter One Of:",possible_choices

# 2. run a whole quiz (ask all our questions)
def do_quiz():
    answers = []
    for question_text in questions_list:
        answer = ask_question(question_text,['a','b','c','d','quit','restart')
        if answer == "restart":
           return False
           # instead of returning false we could also simply call recursively
           # return do_quiz()
        elif answer == "quit":
           print "Adios muchacho"
           sys.exit(0)
        else:
           answers.append(answer)
    return answers

# 3. play forever (or atleast until the user wants to quit...)
while True:
    results = do_quiz()
    if not results:
       print "Lets Start Over!!"
       continue
    else:
       check_answers(results)
       if raw_input("Play again?")[0].lower() == "n":
          print "Goodbye!"
          sys.exit(1)
Joran Beasley
  • 110,522
  • 12
  • 160
  • 179