[Tutor] Python program with multiple answers

Prasad, Ramit ramit.prasad at jpmchase.com
Wed May 11 17:33:33 CEST 2011


I think it should be:

import random
dice = ("Without a doubt", "It is certain", "It is decidedly so","Yes", "For Sure", "No", "Dont count on it", "Try asking again","Reply hazy, try again", "Confucious says 'No'", "Better not tell you now","Cannot predict now","Concentrate and ask again","My reply is no","Outlook not so good","Very doubtful","Outlook is good","Most likely","As I see it, yes","I do not understand the question")
answer_set=set([]) # Use set because there is no need to return an answer twice if you are sorting alphabetically
# otherwise use 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")
		roll = random.randint(0, len(dice) -1 )   #-1 because dice[20] will return an IndexError
		answer = dice[roll]
		answer_set.add(answer)
		print answer
		raw_input()
	elif choice == "quit":
		true = 0 # use LOWERCASE as to not attempt redefining True (yes, you can define True to equal False)
		raw_input()
	else:
		print "Error -- Try again\n" # I think print automatically adds \n 
	#answer_list.sort() #this works for a list but not a set
	sorted_list = sorted(answer_set)
	print "Your answer's sorted:", sorted_list # looks ugly
	#try the following for a prettier output
	print "Your answer's sorted: ", ','.join(sorted_list)

Hope that helps,
Ramit



Ramit Prasad | JPMorgan Chase Investment Bank | Currencies Technology
712 Main Street | Houston, TX 77002
work phone: 713 - 216 - 5423


-----Original Message-----
From: tutor-bounces+ramit.prasad=jpmchase.com at python.org [mailto:tutor-bounces+ramit.prasad=jpmchase.com at python.org] On Behalf Of Johnson Tran
Sent: Wednesday, May 11, 2011 8:24 AM
To: tutor at python.org
Subject: Re: [Tutor] Python program with multiple answers

Thanks for all the replies.  But, sorry I am getting a little confused. I have never created a list before and am not really sure where to start. If I put a "answer_list=[]" before the while True: line...is this correct? Or to go by Brett's method, how would I go about saving the questions and answers? If I am only trying to make a list of the answers, I probably do not need to save the questions ?

This is probably completely off but tried:

import random
dice = ("Without a doubt", "It is certain", "It is decidedly so","Yes", "For Sure", "No", "Dont count on it", "Try asking again","Reply hazy, try again", "Confucious says 'No'", "Better not tell you now","Cannot predict now","Concentrate and ask again","My reply is no","Outlook not so good","Very doubtful","Outlook is good","Most likely","As I see it, yes","I do not understand the question")
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")
		roll = random.randint(0, len(dice))
		print dice[roll]
		raw_input()
	elif choice == "quit":
		True = 0
		raw_input()
	else:
		print "Error -- Try again\n"
	answer_list=???  <--not sure 
	answer_list.sort()
	print "Your answer's sorted:", answer_list

On May 11, 2011, at 5:57 AM, Brett Ritter wrote:


* Create a list.
* Each time, the user gets an answer add it to the list
* At the end of the program: sort the list and print each element of it

- Patrick



> On Wed, May 11, 2011 at 6:49 AM, Johnson Tran <aznjonn at me.com> wrote:
>> I've been working on a Python program where I create an 8 ball that will allow you to ask questions and will reply back with 20 possible answers.
> ...
>> if anyone could point me in the right direction it'd be really helpful
> 
> Answer cloudy, try again later [couldn't resist]
> 
> Patrick gave a decent summary. I'd suggest for learning purposes take
> each step at a time:
> 1) Save the questions, then once that works:
> 2) Save the corresponding answers, then
> 3) Print them, then
> 4) Sort them
> 
> That way if you encounter any problem it's limited in scope rather
> than trying to take it all in at once.
> -- 
> Brett Ritter / SwiftOne
> swiftone at swiftone.org



_______________________________________________
Tutor maillist  -  Tutor at python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor
This communication is for informational purposes only. It is not
intended as an offer or solicitation for the purchase or sale of
any financial instrument or as an official confirmation of any
transaction. All market prices, data and other information are not
warranted as to completeness or accuracy and are subject to change
without notice. Any comments or statements made herein do not
necessarily reflect those of JPMorgan Chase & Co., its subsidiaries
and affiliates.

This transmission may contain information that is privileged,
confidential, legally privileged, and/or exempt from disclosure
under applicable law. If you are not the intended recipient, you
are hereby notified that any disclosure, copying, distribution, or
use of the information contained herein (including any reliance
thereon) is STRICTLY PROHIBITED. Although this transmission and any
attachments are believed to be free of any virus or other defect
that might affect any computer system into which it is received and
opened, it is the responsibility of the recipient to ensure that it
is virus free and no responsibility is accepted by JPMorgan Chase &
Co., its subsidiaries and affiliates, as applicable, for any loss
or damage arising in any way from its use. If you received this
transmission in error, please immediately contact the sender and
destroy the material in its entirety, whether in electronic or hard
copy format. Thank you.

Please refer to http://www.jpmorgan.com/pages/disclosures for
disclosures relating to European legal entities.


More information about the Tutor mailing list