CSC111 Lab 9
This lab deals with while loops. You may find the exercises we did in class yesterday (and their solutions) useful for some of the questions below!
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 loop 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...