Re: [Tutor] dealing with user input whose value I don't know

2008-10-05 Thread Omer
On Fri, Oct 3, 2008 at 9:45 AM, David [EMAIL PROTECTED] wrote: Here is the code: def main(): import string Hey, lagging a bit behind the list, import string is unnecessary, mate. ___ Tutor maillist - Tutor@python.org

Re: [Tutor] dealing with user input whose value I don't know

2008-10-05 Thread Alan Gauld
Omer [EMAIL PROTECTED] wrote Here is the code: def main(): import string import string is unnecessary, mate. Not entirely true since the code uses string.split() However since the split method of the string could be used instead then that would indeed render the import unnecessary.

Re: [Tutor] dealing with user input whose value I don't know

2008-10-03 Thread David
Hello Alan, dear list members, Alan Gauld wrote: The solution you have already seen - use string.split(',') to separate the string into substrings and then convert each substring to an integer. This I have now done by using eval(). But now I wonder whether that is actually clever because it is

Re: [Tutor] dealing with user input whose value I don't know

2008-10-03 Thread Alan Gauld
David [EMAIL PROTECTED] wrote the string into substrings and then convert each substring to an integer. This I have now done by using eval(). But now I wonder whether that is actually clever because it is supposed to be similarly problematic as the input() function in terms of security.

[Tutor] dealing with user input whose value I don't know

2008-10-02 Thread David
Hello, I am trying to do some exercises in John Zelle's book (chapter 4). I got stuck: Write a program that finds the average of a series of numbers entered by the user. The program should first ask the user how many numbers there are. Note: the average should always be a float, even if the

Re: [Tutor] dealing with user input whose value I don't know

2008-10-02 Thread Luke Paireepinart
On Thu, Oct 2, 2008 at 12:06 PM, David [EMAIL PROTECTED] wrote: Hello, I am trying to do some exercises in John Zelle's book (chapter 4). I got stuck: Write a program that finds the average of a series of numbers entered by the user. The program should first ask the user how many numbers

Re: [Tutor] dealing with user input whose value I don't know

2008-10-02 Thread Steve Willoughby
On Fri, Oct 03, 2008 at 01:06:29AM +0800, David wrote: Hello, I am trying to do some exercises in John Zelle's book (chapter 4). I got stuck: Okay, I can ask how many number are to be added: numbers = input(How many number do you want me to calculate? ) If I then get a reply, say 5,

Re: [Tutor] dealing with user input whose value I don't know

2008-10-02 Thread christopher . henk
[EMAIL PROTECTED] wrote on 10/02/2008 01:06:29 PM: Hello, I am trying to do some exercises in John Zelle's book (chapter 4). I got stuck: Write a program that finds the average of a series of numbers entered by the user. The program should first ask the user how many numbers there

Re: [Tutor] dealing with user input whose value I don't know

2008-10-02 Thread David
Hello Christopher, [EMAIL PROTECTED] wrote: Okay, I can ask how many number are to be added: numbers = input(How many number do you want me to calculate? ) you should really use raw_input to get the info from the user, and then convert it to a number.

Re: [Tutor] dealing with user input whose value I don't know

2008-10-02 Thread Steve Willoughby
On Fri, Oct 03, 2008 at 01:38:48AM +0800, David wrote: Does that mean input() is obsolete (after all, Zelle's book is not the freshest on the shelf)? Or do they have different uses? Depends on how you look at it. input() automatically evaluates whatever the user types as a Python expression

Re: [Tutor] dealing with user input whose value I don't know

2008-10-02 Thread Bill Campbell
On Thu, Oct 02, 2008, Steve Willoughby wrote: On Fri, Oct 03, 2008 at 01:38:48AM +0800, David wrote: Does that mean input() is obsolete (after all, Zelle's book is not the freshest on the shelf)? Or do they have different uses? Depends on how you look at it. input() automatically evaluates

Re: [Tutor] dealing with user input whose value I don't know

2008-10-02 Thread David
Cheers for the insights! However, I just found out that changing input() to raw_input() breaks my code: This program takes the average of numbers you supply!! How many numbers do you want me to work with? 2 You want me to take the average of 2 numbers. Please type the numbers, separated by

Re: [Tutor] dealing with user input whose value I don't know

2008-10-02 Thread Steve Willoughby
On Thu, Oct 02, 2008 at 10:54:56AM -0700, Bill Campbell wrote: Remember the cardinal rule NEVER TRUST USER INPUT! Always check for validity, and use methods that prevent malicious strings from allowing the user to get unauthorized access or change things they shouldn't. Yes, I probably

Re: [Tutor] dealing with user input whose value I don't know

2008-10-02 Thread Steve Willoughby
On Fri, Oct 03, 2008 at 02:06:47AM +0800, David wrote: Cheers for the insights! However, I just found out that changing input() to raw_input() breaks my code: Recall that we told you raw_input() returns a string, while input() returns an integer if you typed an integer value. So you need

Re: [Tutor] dealing with user input whose value I don't know

2008-10-02 Thread christopher . henk
one at a time inside the loop. add = add + int(raw_input(Please type the next number:)) Chris David [EMAIL PROTECTED] Sent by: [EMAIL PROTECTED] 10/02/2008 02:06 PM To tutor@python.org, [EMAIL PROTECTED] cc Subject Re: [Tutor] dealing with user input whose value I don't know Cheers

Re: [Tutor] dealing with user input whose value I don't know

2008-10-02 Thread David
Hello Steve, thanks for all your help and comments. What happens, though, is that with numbers = int(raw_input(Please type the numbers, separated by commas: )) my code is still defunct (whereas input() works): Please type the numbers, separated by commas: 1,2 Traceback (most recent call

Re: [Tutor] dealing with user input whose value I don't know

2008-10-02 Thread David
, [EMAIL PROTECTED] cc Subject Re: [Tutor] dealing with user input whose value I don't know Cheers for the insights! However, I just found out that changing input() to raw_input() breaks my code: This program takes the average of numbers you supply!! How many numbers do

Re: [Tutor] dealing with user input whose value I don't know

2008-10-02 Thread Alan Gauld
David [EMAIL PROTECTED] wrote However, I just found out that changing input() to raw_input() breaks my code: You want to know the average of the numbers: 1,2 Traceback (most recent call last): File avgInput.py, line 13, in module add = add + i TypeError: unsupported operand type(s) for

Re: [Tutor] dealing with user input whose value I don't know

2008-10-02 Thread Alan Gauld
David [EMAIL PROTECTED] wrote Does that mean input() is obsolete (after all, Zelle's book is not the freshest on the shelf)? Or do they have different uses? They have different uses and input is very convenient at the prompt or when experimenting but in most cases is the wrong choice for