[Tutor] Python program with multiple answers

Alan Gauld alan.gauld at btinternet.com
Wed May 11 17:24:28 CEST 2011


"Johnson Tran" <aznjonn at me.com> wrote

> If I put a "answer_list=[]" before the while True: line...is this 
> correct?

Yes that creates an empty list.

> only trying to make a list of the answers, I probably
> do not need to save the questions ?

Correct, but....

> import random
> ....
> answer_list=[]
> while True:
>         choice = raw_input("Type 'ask' to ask a question. Type 
> 'quit' to quit.\n")
>          if choice == "ask":
>              raw_input("Please enter your question:\n")

You are not storing the users input so how do you know
which question was input? I'd expect to see:

question = raw_input("Please enter your question:\n")

Or is this really just a print statement? You seem to ignore
this value and just print a random question...

> roll = random.randint(0, len(dice))
> print dice[roll]
> raw_input()

Again this does not store the answer. I'd expect:

answer_list.append( raw_input() )

This will add the answer to the list

> elif choice == "quit":

         break

> else:
>          print "Error -- Try again\n"
> answer_list.sort()
> print "Your answer's sorted:", answer_list

That will print the list ijncluding brackets etc, you probably want:

print "your answers sorted:"
for answer in answer_list: print answer

HTH,

-- 
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/




More information about the Tutor mailing list