[Tutor] A question

Alan Gauld alan.gauld at yahoo.co.uk
Thu Apr 2 06:56:14 EDT 2020


On 02/04/2020 11:00, Alan Gauld via Tutor wrote:

> You should be able to restructure your function so you only do things
> once(or at most twice!).

OK, I was bored. So came up with this:


########################
import sys

AnswerList = "a-Biology\nb-Chemistry\nc-Math\nd-Physics\ne-Business
Studies\nf-Computer Science\ng-Art".split('\n')

def question(query,options):
    print(query + "\n" + "\n".join(options))
    answer = input("Enter your answer's letter or X to exit: ").lower()
    tries = 0
    while tries < 2:
       tries += 1
       if answer in [a[0] for a in options] :
           return answer
       elif answer == 'x':
           print("Goodbye...")
           sys.exit(0)
       else:
            print("\nThe answer", answer, "is invalid, you have %d more
tries.\nIf you wish to exit the program enter X" % (3-tries))
            answer=input("Enter your answer: ")
    else:
       print("The answer", answer, "is still invalid.\nThe program is
being terminated as you have reached your maximum number of tries")
       sys.exit(0)

choice = question("Q1) Which of the following subjects do you find most
interesting:", AnswerList)

print("\n\nThe answer was:", choice)
###################

But that doesn't explain what you were really asking about
passing parameters to AnswerCollection()

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos




More information about the Tutor mailing list