[Tutor] counting number of inputs (EARLIER VERSION SENT ACCIDENTLY)
MICHELLE EVANS
evans1018 at verizon.net
Wed May 3 21:33:54 CEST 2006
OK, I've tried a different approach to this.
How do I get this to stop by using -1?
I do not want this to print until either 5 inputs have been entered or -1
has been entered. See below:
# Add number of per hour
numbers = []
stop = None
while stop != "-1":
number = int(raw_input("Run number(-1 to end) : "))
numbers.append(number)
print
for number in numbers:
print number
----- Original Message -----
From: "Python" <python at venix.com>
To: "MICHELLE EVANS" <evans1018 at verizon.net>
Cc: "Tutor Python" <tutor at python.org>
Sent: Wednesday, May 03, 2006 12:18 PM
Subject: Re: [Tutor] counting number of inputs (EARLIER VERSION SENT
ACCIDENTLY)
> (Tip: Best to use reply-to-all when responding to an email on the list)
> On Tue, 2006-05-02 at 21:34 -0400, MICHELLE EVANS wrote:
> > number1 = int(raw_input("Run number 1 (-1 to end) : "))
> > number2 = int(raw_input("Run number 2 (-1 to end) : "))
> > number3 = int(raw_input("Run number 3 (-1 to end) : "))
> > number4 = int(raw_input("Run number 4 (-1 to end) : "))
> > number5 = int(raw_input("Run number 5 (-1 to end) : "))
> Good. You collect the string from raw_input and convert it to an
> integer.
>
> This will prompt for 5 inputs, but it is missing any logic to actually
> break if -1 is entered. With a language like BASIC, you could stick in
> tests sort of like:
> if number1 == -1 goto done:
> BUT Python does not have a goto. So we actually need some "flow
> control" around the block of code where you collect inputs.
>
> while blocks process an indefinite number of times while a test
> condition is True.
>
> for blocks iterate through a sequence until they reach the end. By
> providing a sequence with the correct count, you can repeat the block
> the correct number of times. The range (and xrange for big sequences)
> functions provide a sequence of integers that can be used conveniently
> with for.
>
> The easiest way to fix your code above would be something like:
> ask_for_number = True
> while ask_for_number:
> number1 = ....
> if number1 == -1: break
> ...
> number5 = ...
> ask_for_number = False
>
> HOWEVER, that is not a good approach in the long run.
>
> A better approach is to have a single container to hold all of the
> inputs. For this, Python provides lists. Rather than have 5 separate
> variables, use a single list variable to hold all of the inputs. Then
> use a "for block" to ask for the input and put the result into the list.
> You already know how to convert the input from a string to a number.
>
> If you have trouble figuring out lists and for blocks, ask for help.
>
> (Sorry about the extra email. I forgot and used ad editor hot-key combo
> in my email program which sent the email.)
>
>
> >
> >
> > # The following will sum the numbers and then print the answer
> > sum = number1 + number2 + number3 + number4 + number5
> > print
> > print "The total number of parts produced was:", sum,"."
> >
> > I need this to ask the user to enter their number per each run. That is
why
> > I have 5 different input numbers. I need this break if a -1 is entered.
> > Would I use "if-else" to break this if -1 is entered? I need to be able
to
> > count the number of lines entered.
> >
> > Thanks
> > Rick
> >
> >
> > ----- Original Message -----
> > From: "Python" <python at venix.com>
> > To: "MICHELLE EVANS" <evans1018 at verizon.net>
> > Cc: "Tutor Python" <tutor at python.org>
> > Sent: Tuesday, May 02, 2006 7:56 PM
> > Subject: Re: [Tutor] counting number of inputs
> >
> >
> > > On Tue, 2006-05-02 at 19:25 -0400, MICHELLE EVANS wrote:
> > > > I am trying to count the number of times a positive number is
entered
> > > > from the user. But, the program must stop after 5 user inputs or a
> > > > negative number.
> > > >
> > > > Can anyone help.
> > > Yes, but you need to help yourself also.
> > >
> > > Do you know how to get input from the user?
> > > Do you know how to count things in Python?
> > > Do you know how to test a number to see if it is positive or negative?
> > >
> > > Why don't you post your code for any part of this problem and explain
> > > how it is supposed to work and where you are having difficulty. If
> > > necessary, review some of the tutorials to get some pointers on
writing
> > > Python programs.
> > >
> > > We're happy to help you learn, but do not want to simply write your
> > > program for you.
> > >
> > > > Rick
> > > > _______________________________________________
> > > > Tutor maillist - Tutor at python.org
> > > > http://mail.python.org/mailman/listinfo/tutor
> > > --
> > > Lloyd Kvam
> > > Venix Corp
> > >
> > >
> >
> --
> Lloyd Kvam
> Venix Corp
>
>
More information about the Tutor
mailing list