[Tutor] counting number of inputs
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. Rick ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Fw: counting number of inputs
This is what I have so far. Can anyone help? > 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) : "))> > > # 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. I also need to print the run number with the highest and lowest input number.> > Thanks, I'm lost> Rick> - Original Message - From: MICHELLE EVANS To: tutor@python.org Sent: Tuesday, May 02, 2006 7:25 PM Subject: counting number of inputs 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. Rick ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] counting number of inputs (EARLIER VERSION SENT ACCIDENTLY)
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" <[EMAIL PROTECTED]> To: "MICHELLE EVANS" <[EMAIL PROTECTED]> Cc: "Tutor Python" 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" <[EMAIL PROTECTED]> > > To: "MICHELLE EVANS" <[EMAIL PROTECTED]> > > Cc: "Tutor Python" > > 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@python.org > > > > http://mail.python.org/mailman/listinfo/tutor > > > -- > > > Lloyd Kvam > > > Venix Corp > > > > > > > > > -- > Lloyd Kvam > Venix Corp > > ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] counting number of inputs (EARLIER VERSION SENT ACCIDENTLY)
This is exactly what I am trying to do. I am so confused with trying to write this. I am not very familiar with any of the functions. I keep reading my book and reading my book, and none of it seems to make sense anymore. I can write extremely simple functions, but when I need to use more than one in a code, I'm lost! Thanks - Original Message - From: "Marc Poulin" <[EMAIL PROTECTED]> To: Sent: Thursday, May 04, 2006 12:08 AM Subject: Re: [Tutor] counting number of inputs (EARLIER VERSION SENT ACCIDENTLY) > > Michelle: > > Are you familiar with writing functions? > Here I've created a function named getInputs. > > I've also created a few test cases to verify that (a) > my understanding of the problem is correct, and (b) my > solution is correct. > > It's important to think about how your program is > supposed to behave in different situations. Do you > think these 3 tests are enough to prove that the code > is correct? > > # > ## start of code ## > # > def getInputs(): > """ > Description: >Collect numbers entered by the user (up to a > maximum of 5 values) and >store them in the listOfValues. > >Stop collecting numbers if the user enters -1 > or if 5 numbers have been collected. > >If the user entered -1, the -1 is NOT returned > as part of the list. > """ > listOfValues = [] ## this list holds the values > entered by the user > > for i in range(5): > newValue = int(raw_input('Enter a number [-1 > to exit]:')) > if newValue == -1: > # Return right now with whatever is > currently in the list. > return listOfValues > else: > # Add this new value to the list and keep > looping. > listOfValues.append(newValue) > > ## If we got this far, it means the user did not > enter a -1 so > ## the list contains 5 values. > return listOfValues > > """ > Here are a few test cases to verify the logic of my > code. > > Test Case 1: >INPUTS: > first entered value: -1 >RESULT: > function returns empty list > > Test Case 2: >INPUTS: > first entered value: 1 > second entered value: 2 > third entered value: -1 >RESULT: > returned list contains [1,2] > > Test Case 3: >INPUTS: > first entered value: 1 > second entered value: 2 > third entered value: 3 > fourth entered value: 4 > fifth entered value: 5 >RESULT: > returned list contains [1,2,3,4,5] > """ > if __name__ == "__main__": > print getInputs() > > ### > ## end of code ## > ### > > > --- Python <[EMAIL PROTECTED]> wrote: > > > On Wed, 2006-05-03 at 15:33 -0400, MICHELLE EVANS > > wrote: > > > 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: > > > > > > > use a "for block" rather than a "while block" to > > have a normal limit of > > 5 repetitions: > > > > for x in range(5): > > > > will repeat 5 times with x running from 0 to 4. > > x is ignored - unless some use for it does turn up. > > > > the break statement allows you to terminate a block, > > so > > > > if number == -1: break > > > > will end the for block. > > > > > > Now, one of the cute features in Python is the else > > clause that goes > > with the for and while blocks. The else block is > > executed when there is > > no break. So the skeleton for your program can look > > something like > > > > for x in range(5): > > # get inputs and break on -1 > > else: > > # no break so just process the inputs > > > > Good luck. > > > > > # 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" <[EMAIL PROTECTED]> >