CSC111 Lab 9
While Loops
Loop 1
Create the following program:
def main():
print "Enter a number between 3 and 9: "
x = input( "> " )
while not ( 3 <= x <= 9 ):
print "invalid input!"
x = input( "> " )
print "x = ",x
main()
- Verify that it runs correctly and keeps on prompting the user as long as the input is not between 3 and 9.
- Put the input and the while statements inside a function which you will call input3_9(), which will return the number entered by the user, and which will keep on prompting the user for a new number as long as it is not in the valid range. Your main program should look something like this:
def main():
x = input3_9()
print "x = ", x
main()
Loop 2
- Using input3_9() as an example, create another function called inputDollars() that asks the user to enter
a number and returns only numbers that are multiples of 5. Below is what your main program will look like.
- Make sure your new function is robust and accepts only numbers of the form 0, 5, 10, 15, 20, etc.
def main():
x = input3_9()
print "x = ", x
dollars = inputDollars()
print "dollars = ", dollars
main()
More challenging Loop 3
- Modify your function (or add a new one), such that it will only accept from the user numbers of the form 5, 10, 20, 50, and 100.
(Hints: Remember that lists are easy to create, and that the "in" operator can be used as a boolean operator, returning True if an item is in a list, and False otherwise...)
One-and-a-half While loops
Another way to write the loop of the previous example is to do this:
while True:
x = input( "> " )
if <i><x is within the range--you figure out how to write it></I>
break
print "Invalid input"
Modify your previous program and use this method for implementing robustness.
While loops with sentinels
The program below computes the average of an unknown number of input data.
Store it in a file called sumNum.py and run it. Verify that it gives you the average of the series of numbers you have entered.
def main():
sum = 0
count = 0
print "Enter numbers (enter a negative number to quit)"
x = input( "> " )
while ( x >= 0 ):
sum = sum + x
count = count + 1
x = input( "> " )
if ( count>0 ):
print "\n\naverage = %f" % ( float( sum )/count )
main()
Modify the program and remove the count variable. Instead, keep appending the numbers entered by the user to a list which we'll call numbers for simplicity. If you have a list of numbers, you can easily find how many numbers it contains by using the len() function.
Go ahead and make your program print the average using len( numbers ) instead of count.
Redirected input
We mentioned in class yesterday that sometimes the input is not provided at the keyboard, but from a file.
We'll see how to feed data to your program by not typing it but instead by telling the program to read the contents of a file instead of getting the data from the keyboard.
We are going to use the same program as in the previous question, so make sure it works well!
- Create a file called data.txt using emacs. In it store the following lines:
1 2 3 4 5 -1
- Save the file.
- At the Linux prompt, enter the following command:
python sumNum.py < data.txt
- The output is going to be messed up because the numbers will not be typed at the keyboard and will not appear on the screen, but they will still be absorbed by the program. Verify that the average reported is correct, though!
- You have just discovered how to feed huge amounts of data to your program. Simply write the program such that it reads the information that it needs with input() or raw_input() and when you run your program you redirect its input with the "<" symbol on the command line, and provide the name of a file that contains the data formatted the way the program expects.
While loops and elevators
def main():
people = [ ("JT", 150), ("TJ", 80), ("TT", 120), ("BT", 204), ("Mike", 12), \
("AF", 27), ("Lea", 78), ("Leo", 180), ("Al", 55), ("GK", 110), \
("JR", 111), ("VB", 76) ]
Above is a list of people and their weight. They are waiting in front of an elevator. The maximum weight allowed on the elevator is 300 lbs.
- Question 1
- What is the maximum number of people we can fit in the elevator?
- Question 2
- What is the total weight of these people?
- Question 3 (challenging)
- What is the name of the people in the elevator?