[Tutor] Just started Python

2011-04-27 Thread Johnson Tran
Hi All,

I am a complete beginner so my question will probably be pretty noob but:

I started out with a short program below and I thought it was working although 
I cannot seem to figure out how to use the except ValueError so that when the 
user puts an invalid answer the program does not read with an error. Although 
according to the error message, it seems to be saying that my line 4 "number1 = 
float (number_string1)" is incorrect. Thanks in advance for any advice.

Cheers,

Noob

Program:::
model=raw_input("What kind of car do you drive?")
number_string1=raw_input("How many gallons have you driven?")
number1 = float (number_string1)
number_string2=raw_input("How many miles have you driven?")
number2 = float (number_string2)


try:
model=float(model)
except ValueError:
pass
print "Your average number of miles to gallons is",
print number1 / number2
What kind of car do you drive?firebird
How many gallons have you driven?test


Output of Program::
T>>>  RESTART 
>>> 
What kind of car do you drive?firebird
How many gallons have you driven?30
How many miles have you driven?60
Your average number of miles to gallons is 0.5
>>>  RESTART 
>>> 
What kind of car do you drive?firebird
How many gallons have you driven?test

Traceback (most recent call last):
  File "/Users/JT/Desktop/test", line 4, in 
number1 = float (number_string1)
ValueError: invalid literal for float(): test
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Just started Python

2011-04-27 Thread Johnson Tran
Thanks for the reply Alan and Noah, I really appreciate the help. I am really 
trying to understand this although still cannot seem to grasp it all. I have 
modified my program although now it seems to be giving me the wrong answer with 
the correct answer when I input any value.

I have program:

model=raw_input("What kind of car do you drive?")
gallons=raw_input("How many gallons have you driven?")
number1 = float (gallons)
miles=raw_input("How many miles have you driven?")
number2 = float (miles)


try:
   model=float(model)
except ValueError:
   print "I cannot compute your total miles to gallon with those values."
else:
   print "Your average number of miles to gallons is",
print number1 / number2

Output:

What kind of car do you drive?fire
How many gallons have you driven?10
How many miles have you driven?5
I cannot compute your total miles to gallon with those values.


On Apr 27, 2011, at 10:02 AM, Alan Gauld wrote:

> 
> "Johnson Tran"  wrote
> 
>> I started out with a short program below and I thought it was working 
>> although I cannot seem to figure out how to use the except ValueError so 
>> that when the user puts an invalid answer the program does not read with an 
>> error. 
> 
> You have to replace the line that says 'pass' with code that does something 
> to make the value correct. Asking the user to try again with a more sensible 
> value would be a start.
> 
>> Although according to the error message, it seems to be saying that my line 
>> 4 "number1 = float (number_string1)" is incorrect. 
> 
> Its not saying its incorrect, its saying thats where the ValueError occured. 
> Which is true because you entered 'test' which cannot be converted to a float 
> - its an invalid value.
> 
>> model=raw_input("What kind of car do you drive?")
>> number_string1=raw_input("How many gallons have you driven?")
>> number1 = float (number_string1)
>> number_string2=raw_input("How many miles have you driven?")
>> number2 = float (number_string2)
> 
> You will fin it easier if you use descriptive names for your variables.
> 
> gallons and miles 
> would seem reasonable here...
> 
>> try:
>>   model=float(model)
>> except ValueError:
>>   pass
> 
> This says if you get an error ignore it (ie pass).
> But you don't want to ignore it, you want to get a valid value.
> 
>> print "Your average number of miles to gallons is",
>> print number1 / number2
> 
> HTH,
> 
> 
> -- 
> Alan Gauld
> Author of the Learn to Program web site
> http://www.alan-g.me.uk/
> 
> 
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Just started Python

2011-04-27 Thread Johnson Tran
Yeah, I'll be honest I did not really understand the float concept, thanks for 
explaining that =). So basically model does not have to be defined in try: 
because it does not need to be told to be put into point number?

Okay so my final program is:

model=raw_input("What kind of car do you drive?")
gallons=raw_input("How many gallons have you driven?")
miles=raw_input("How many miles have you driven?")

try:
number1 = float(gallons)
number2 = float(miles)
except ValueError:
print "some values are wrong type."
else:
print "Your average number of miles to gallons is",
print number1 / number2


Thanks guys created my first successful program ! =D







On Apr 27, 2011, at 3:13 PM, Alan Gauld wrote:

> 
> "Johnson Tran"  wrote
> 
>> Thanks for the reply Alan and Noah, I really appreciate
>> the help. I am really trying to understand this although
>> still cannot seem to grasp it all.
> 
> Lets step back a stage.
> 
> Do you understand what float() does?
> 
>> I have modified my program although now it seems
>> to be giving me the wrong answer with the correct
>> answer when I input any value.
>> 
>> model=raw_input("What kind of car do you drive?")
>> gallons=raw_input("How many gallons have you driven?")
>> number1 = float (gallons)
> 
> This is taking the value typede by the user and trying
> to convert it from a string to a floating point number - that is,
> a decimal value
> 
>> miles=raw_input("How many miles have you driven?")
>> number2 = float (miles)
> 
> Similarly this converts the input string to a decimal value,
> if possible.
> 
>> try:
>>  model=float(model)
> 
> But what is this doing?
> 
> It is trying to convert the model of car to a decimal value.
> Unless trhe car is a Porche 911 or similar its unlikely
> to succeed! If its a Toyota Prius it will fail with a ValueError
> exception.
> 
>> except ValueError:
>>  print "I cannot compute your total miles to gallon with those values."
> 
> And therefore print this message
> 
>> else:
>>  print "Your average number of miles to gallons is",
>> print number1 / number2
> 
> The second print needs to be inside the else too.
> 
>> What kind of car do you drive?fire
>> How many gallons have you driven?10
>> How many miles have you driven?5
>> I cannot compute your total miles to gallon with those values.
> 
> Because it cannot convert 'fire' to a decimal value.
> 
> HTH,
> 
> 
> -- 
> Alan Gauld
> Author of the Learn to Program web site
> http://www.alan-g.me.uk/
> 
> 
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Just started Python

2011-04-27 Thread Johnson Tran
Ahh, I was wondering about why it didn't show the error message on the first 
variable. Makes sense, thanks again.

Sent from my iPhone

On Apr 27, 2011, at 5:05 PM, Alan Gauld  wrote:

> 
> "Johnson Tran"  wrote 
>> Okay so my final program is:
> 
> It works but you can improve it slightly with a simple change.
> 
>> model=raw_input("What kind of car do you drive?")
>> gallons=raw_input("How many gallons have you driven?")
>> miles=raw_input("How many miles have you driven?")
>> try:
>>   number1 = float(gallons)
>>   number2 = float(miles)
> 
> Combine these two groups of lines so that you detect the errors as soon as 
> possible.:
> 
> try:
>  model=raw_input("What kind of car do you drive?")
>  gallons=raw_input("How many gallons have you driven?")
>  number1 = float(gallons)
>  miles=raw_input("How many miles have you driven?")
>  number2 = float(miles)
> 
> Now the user gets prompted as soon as they enter a wrong number, they don't 
> need to guess which one was wrong...
> 
> except ValueError:
>   print "That value is the wrong type, please use a number."
> 
> And so we can make the message more specific too.
> 
>> else:
>>   print "Your average number of miles to gallons is",
>>   print number1 / number2
> 
> HTH,
> 
> -- 
> Alan Gauld
> Author of the Learn to Program web site
> http://www.alan-g.me.uk/
> 
> 
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] ValueError

2011-05-03 Thread Johnson Tran
Hi All, 

i am trying to create a program module with two functions (conversion inches to 
centimeters then centimeter to inches, I have my program working although I am 
trying to adda Value Error function to my program but cannot seem to it to work:


def Conversion():
print "This program converts the first value from inches to centimeters and 
second value centimeters to inches."
print "(1 inch = 2.54 centimeters)"
inches = input("Enter length in inches: ")
centimeters = 2.54 * inches
print "That is", centimeters, "centimeters."

centimeters = input("Enter length in centimeters: ")
inch = centimeters / 2.54
print "That is", inch, "inches."

except ValueError:
print "Invalid digit, please try again."

Conversion()



Any advice would be great, thanks!

JT
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] ValueError

2011-05-03 Thread Johnson Tran
Thanks for the replies..so I added the "try" block but it still does not seem 
to be outputting my default error message:

def Conversion():
try:

print "This program converts the first value from inches to centimeters 
and second value centimeters to inches."
print "(1 inch = 2.54 centimeters)"
inches = input("Enter length in inches: ")
centimeters = 2.54 * float(inches)
print "That is", centimeters, "centimeters."
centimeters = input("Enter length in centimeters: ")
inch = float(centimeters) / 2.54
print "That is", inch, "inches."

except ValueError:
 print "Invalid digit, please try again." 
Conversion()

Error message:

Traceback (most recent call last):
  File "/Users/JT/Desktop/hw#2.py", line 16, in 
Conversion()
  File "/Users/JT/Desktop/hw#2.py", line 9, in Conversion
centimeters = input("Enter length in centimeters: ")
  File "", line 1, in 
NameError: name 'fs' is not defined


On May 3, 2011, at 4:00 AM, Corey Richardson wrote:

> -BEGIN PGP SIGNED MESSAGE-
> Hash: SHA1
> 
> On 05/03/2011 06:30 AM, Johnson Tran wrote:
>> Hi All, 
>> 
>> i am trying to create a program module with two functions (conversion inches 
>> to centimeters then centimeter to inches, I have my program working although 
>> I am trying to adda Value Error function to my program but cannot seem to it 
>> to work:
>> 
>> 
>> def Conversion():
>>print "This program converts the first value from inches to centimeters 
>> and second value centimeters to inches."
>>print "(1 inch = 2.54 centimeters)"
>>inches = input("Enter length in inches: ")
>>centimeters = 2.54 * inches
>>print "That is", centimeters, "centimeters."
>> 
>>centimeters = input("Enter length in centimeters: ")
>>inch = centimeters / 2.54
>>print "That is", inch, "inches."
>> 
>>except ValueError:
>>print "Invalid digit, please try again."
>> 
>> Conversion()
>> 
>> 
>> 
>> Any advice would be great, thanks!
>> 
>> JT
> 
> Well, you need a 'try' block before that except. Example:
> 
> try:
>foo = int("blargh")
> except ValueError:
>pass
> 
> It looks like you're forgetting an important operation on the inches
> variable, as well as centimeters later on.
> 
> Take a look at
> http://docs.python.org/library/stdtypes.html#typesnumeric
> 
> For future questions, it's best to include what your program /is/ doing
> that you think it shouldn't be, as well as any errors (tracebacks, copy
> the whole thing!) you get.
> 
> - -- 
> Corey Richardson
> -BEGIN PGP SIGNATURE-
> Version: GnuPG v2.0.17 (GNU/Linux)
> Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/
> 
> iQEcBAEBAgAGBQJNv+BHAAoJEAFAbo/KNFvpCdwH/0DRxVjevNxZy2HtYuxykzlA
> x2ni1VnDMyS2YCsHvqIaglfK2hBeL+nstGL8kmEhGu4t5Z85nqGt9Ea2spPhPDxE
> UJJ1O2nYFtLUZ1BC03vkC8aHI0aiijZjg7v7adKW4sD2laGTaeryLLR1qbGh3ZBP
> rKTWK/NuyyMDRYjnP0gXsiYYNPc6E6WsbBTYWxGcMPwLnlvgmmXJBOLC6qj07eXu
> X/fd5FwKSRJPMYIGT47zsnFdZdrN1SOOM537XH8CX+xQPTg/J8NeaiqoaWjTKLMn
> PpXizt1AbuV1/0/Zqp6VKgTA/sxYtMfc4mFWjfovHlxJ/ahA19DaQjeWneIjldk=
> =LkuV
> -END PGP SIGNATURE-
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] ValueError

2011-05-03 Thread Johnson Tran
I am using python 2.5...and adding raw_input has fixed the issue, thank you!

On May 3, 2011, at 5:01 AM, Peter Otten wrote:

> Johnson Tran wrote:
> 
>> Thanks for the replies..so I added the "try" block but it still does not
>> seem to be outputting my default error message:
>> 
>> def Conversion():
>>try:
>> 
>>print "This program converts the first value from inches to
>>centimeters and second value centimeters to inches." print "(1
>>inch = 2.54 centimeters)" inches = input("Enter length in inches:
>>") centimeters = 2.54 * float(inches)
>>print "That is", centimeters, "centimeters."
>>centimeters = input("Enter length in centimeters: ")
>>inch = float(centimeters) / 2.54
>>print "That is", inch, "inches."
>> 
>>except ValueError:
>> print "Invalid digit, please try again."
>> Conversion()
>> 
>> Error message:
>> 
>> Traceback (most recent call last):
>>  File "/Users/JT/Desktop/hw#2.py", line 16, in 
>>Conversion()
>>  File "/Users/JT/Desktop/hw#2.py", line 9, in Conversion
>>centimeters = input("Enter length in centimeters: ")
>>  File "", line 1, in 
>> NameError: name 'fs' is not defined
> 
> input() in Python 2.x tries to evaluate your input as a Python expression, 
> so if you enter "2*2" it gives you 4, and when you enter "fs" it tries to 
> look up the value of a global variable "fs" in your python script. You don't 
> have such a variable in your script, so it complains with a NameError.
> 
> The best way to avoid such puzzling behaviour is to use raw_input() instead 
> of input().
> 
> Also you should make the try...except as narrow as possible
> 
> try:
>centimeters = float(centimeters)
> except ValueError as e:
>print e
> 
> is likely to catch the float conversion while with many statements in the 
> try-suite you are more likely to hide a problem that is unrelated to that 
> conversion.
> 
> PS: In Python 3.x raw_input() is gone, but input() behaves like raw_input() 
> in 2.x
> 
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Python program with multiple answers

2011-05-11 Thread Johnson Tran
Hi Guys, 

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. It will be 
continuous until the user says quit. My program works fine although I am trying 
to add something more to it, where when the user quits, it will present all the 
answers that the user got again and display them in alphabetical order...if 
anyone could point me in the right direction it'd be really helpful...my 
program so far is : (which works fine with no errros)



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")
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, 10)
print dice[roll]
raw_input()
elif choice == "quit":
True = 0
raw_input()
else:
print "Error -- Try again\n"





Thanks,

JT

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Python program with multiple answers

2011-05-11 Thread Johnson Tran
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  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
> swift...@swiftone.org



___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Just Joined!

2011-05-13 Thread Johnson Tran
Okay I think I understand now. Is there a module i need to import though when 
defining ?I did a help("module"), not sure which module is the correct one to 
make swapcase.center work properly...


On May 12, 2011, at 10:17 AM, Alan Gauld wrote:

> 
> "Alex Smith"  wrote
>> SwapcaseAndCenter('hello', 10) I don't understand how from your example you 
>> get an output ...I get the below error:
>> NameError: name 'SwapcaseAndCenter' is not defined
> 
> Wayne was showing how it should work. As Python says the function is not 
> defined yet.
> 
> Defining the function so that is does do what Wayne showed is your homework! 
> :-)
> 
> -- 
> Alan Gauld
> Author of the Learn to Program web site
> http://www.alan-g.me.uk/
> 
> 
> 
> 
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Just Joined!

2011-05-13 Thread Johnson Tran
You can ignore last question, was trying to figure out Alex's issue as well, 
but I got it now. 

sorry for the spam!


On May 13, 2011, at 3:57 AM, Johnson Tran wrote:

> Okay I think I understand now. Is there a module i need to import though when 
> defining ?I did a help("module"), not sure which module is the correct one to 
> make swapcase.center work properly...
> 
> 
> On May 12, 2011, at 10:17 AM, Alan Gauld wrote:
> 
>> 
>> "Alex Smith"  wrote
>>> SwapcaseAndCenter('hello', 10) I don't understand how from your example you 
>>> get an output ...I get the below error:
>>> NameError: name 'SwapcaseAndCenter' is not defined
>> 
>> Wayne was showing how it should work. As Python says the function is not 
>> defined yet.
>> 
>> Defining the function so that is does do what Wayne showed is your homework! 
>> :-)
>> 
>> -- 
>> Alan Gauld
>> Author of the Learn to Program web site
>> http://www.alan-g.me.uk/
>> 
>> 
>> 
>> 
>> ___
>> Tutor maillist  -  Tutor@python.org
>> To unsubscribe or change subscription options:
>> http://mail.python.org/mailman/listinfo/tutor
> 
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Program

2011-05-18 Thread Johnson Tran
Hi Again All,

I had a couple questions about my program:

def CollectNames():

answer_set=set([])
sorted_list = sorted(answer_set)
word=raw_input("Name #1: ")

word=raw_input("Name #2: ")

word=raw_input("Name #3: ")

word=raw_input("Name #4: ")

word=raw_input("Name #5: ") 

print "Your answer's sorted: ", ','.join(sorted_list)

CollectNames()


1.) how do i add each answer given to the list so it is printed at the end? 
2.) also im trying to modify the program so if the user puts in the same name, 
it will give an make them try again until they have 5 completely different 
names.


Thanks,

JT
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Program

2011-05-18 Thread Johnson Tran
Thanks for the reply.

So to append a word is it suppose to look kind of like:

word=raw_input("Name #1: ")
word.append(words)

I keep getting error message:

Traceback (most recent call last):
  File "/Users/JT/Desktop/pythonfinal", line 23, in 
CollectNames()
  File "/Users/JT/Desktop/pythonfinal", line 7, in CollectNames
word.append(words)
AttributeError: 'str' object has no attribute 'append'

On May 18, 2011, at 5:39 AM, Corey Richardson wrote:

> -BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
> 
> On 05/18/2011 08:26 AM, Johnson Tran wrote:
>> Hi Again All,
>> 
>> I had a couple questions about my program:
>> 
>> def CollectNames():
>> 
>>answer_set=set([])
>>sorted_list = sorted(answer_set)
>>word=raw_input("Name #1: ")
>> 
>>word=raw_input("Name #2: ")
>> 
>>word=raw_input("Name #3: ")
>> 
>>word=raw_input("Name #4: ")
>> 
>>word=raw_input("Name #5: ") 
>> 
>>print "Your answer's sorted: ", ','.join(sorted_list)
>> 
>> CollectNames()
>> 
>> 
>> 1.) how do i add each answer given to the list so it is printed at the end?
> 
> Well, you can't have ALL the answers printed at the end, but one way is
> to use a list and .append(word) each time.
> 
>> 2.) also im trying to modify the program so if the user puts in the same 
>> name, it will give an make them try again until they have 5 completely 
>> different names.
> 
> Now, you might see a pattern in your prompt. Each time you ask for
> input, you increment the name number. Perhaps this is the prime place
> for a loop? If you add in a loop, it will also be fairly easy to add in
> another loop to make sure they enter a name not in the list. So, your
> pseudo-code might look something like this:
> 
> for i in range(6):
>make prompt string;
>get name;
>while name in names_gotten:
>get name;
>add name to names_gotten;
> print names_gotten;
> 
> (P.S., PEP 8 says functions should be lowercase_with_underscore,
> not CamelCase)
> - -- 
> Corey Richardson
> -BEGIN PGP SIGNATURE-
> Version: GnuPG v2.0.17 (GNU/Linux)
> Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/
> 
> iQEcBAEBAgAGBQJN074TAAoJEAFAbo/KNFvpdHwIAK1Ji+4Z3Fac0wtH2EgBDwp2
> K8t10KpbtRYfWOCjiYfBAzZFWLrQ9I+lrmdth7Asf0ANg72U4gPHkp82ZbO8mhyz
> 02eDBPXboAmLcntxsxcmMkNlG1xPVeXjcriGwX/VcN2AguGKvrKkKbkkT+Ar+bWZ
> ZpjH0ycNsAUTNeQLQEHJQJtPMktJ13XvlrjHN0YVoLpk812rAn+nuTZq+p0J5fzc
> hCgyxUiRcHYllXZv/1AegOWbfon3BMur9fpV2UMo8JcsRTHto3Lb5c3jHqApNjfc
> M48rpigGXjOzowj0WbsMmSHrskBglcSAy+xo/Ti0vnBXDMU3secWFWkaxDtdidk=
> =oCrn
> -END PGP SIGNATURE-
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Program

2011-05-19 Thread Johnson Tran
So I figured out how to use the loop method, thanks. I still cannot seem to 
figure out how to use Len() to show the output of my answers (from all my 
googling Len() seems to be used to count characters?) Also, I am not really 
sure I understand how to use the append method of the list.

Here's my best guess so far:


def CollectNames():

for attempt in range(1,6):
word=raw_input("Name #%d" % attempt)
list.append("new")
print "All the names in alphabetical order are ", len(L);

And just to recap, I was trying to get all the names outputted after the last 
name was collected (and sort them in alphabetical order. Took my sort commands 
out until I could figure out how to get any output first


Thanks for any help.

On May 18, 2011, at 7:57 AM, Alan Gauld wrote:

> 
> "Johnson Tran"  wrote
> 
>> I had a couple questions about my program:
>> 
>> def CollectNames():
>>   answer_set=set([])
>>   sorted_list = sorted(answer_set)
> 
> This creates an empty set then sorts it and stores
> the result as an empty list. I'm not sure what you
> think it does but I'm guessing that's not it...
> 
>>   word=raw_input("Name #1: ")
>>   word=raw_input("Name #2: ")
>>   word=raw_input("Name #3: ")
>>   word=raw_input("Name #4: ")
>>   word=raw_input("Name #5: ")
> 
> Do you know about loops yet?
> Any time you find yourself repeating code like
> this think about a loop. A for loop could be
> used here:
> 
> for attempt in range(1,6):
>   word = raw_input("Name #%d" % attempt)
> 
> Although storing all the names in the same variable
> is also probably not what you want. You need to
> add word to your list using the list append() method.
> 
>>   print "Your answers sorted: ", ','.join(sorted_list)
> 
> And this is where you probably want to call sorted()...
> 
>> 1.) how do i add each answer given to the list so it is printed at the end?
> 
> Use the append method of the list
> 
>> 2.) also im trying to modify the program so if the
>> user puts in the same name, it will give an make
>> them try again until they have 5 completely different
>> names.
> 
> A combination of a while loop and a set and the len() function
> might work here. Keep adding to the set while the length of the
> set is <5.
> 
> HTH,
> 
> -- 
> Alan Gauld
> Author of the Learn to Program web site
> http://www.alan-g.me.uk/
> 
> 
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor