This investigation will help you learn how to make a while loop, so that you can repeat a section of instructions over and over again.
Type the following code into WingIDE and run it:
password = "scuba"
print ("Welcome to The Bank!")
print ("For your financial safety, please enter your password to verify your identity")
print ("")
entry = raw_input("Enter your password: ")
while (password != entry):
print ("Incorrect. Please try again.")
print ("")
entry = raw_input("Enter your password: ")
print ("")
print ("Identity Verified. Thank you. You may now complete your banking.")
In the above code, we use a while loop to ensure that the user enters the correct password before we continue.
while
loop similar to an if
statement?while
loop different from an if
statement?while
loop, what is happening on the line entry = raw_input("Enter your password: ")
?entry = raw_input("Enter your password: ")
from inside the while
loop. What happens? Why?Copy the following code into WingIDE and make the changes listed below:
import random
value = random.randint(1, 100)
print("Pick-A-Number")
print("Your computer has chosen a number between 1 and 100. It's your job to find it!")
print("")
guess = int(raw_input("Choose a Number: "))
if (guess < value):
print("Aww... Too low. Better luck next time!")
elif(guess > value):
print("A bit too high! Too bad.")
else:
print("Bingo! You got it. The number was " + str(value))
1. Write a program that simulates a dice roll by picking a random number from 1-6 and then picking a second random number from 1-6. Add the two values together, and display the total. The program should continue to do this until the computer rolls doubles (the same value on both dice).
Note that there is no user input here. So it should happen very quickly.
We don't always have to base our exits on user input. Sometimes we can base it off of an internal counter. Type the following code into WingIDE and run it:
print("Repeater")
print("Type in a message and I'll repeat it 5 times")
message = raw_input("Message: ")
print("")
counter = 0
while (counter < 5):
print (str(counter + 1) + ". " + message)
counter += 1
Normally, while
loops are best for repeating as long as something is true:
But sometimes, we know in advance how many times we want to do something.
We can do that sort of thing with a while
loop, but we have to use a counter. A counter is a number variable (int
or double
) that starts with a value of 0, and then we add 1
to it whenever something happens. So, here, we're going to be adding 1
to the counter every time we repeat the loop. And when the counter reaches a predetermined value, we'll stop looping.
counter += 1
do? Remove it and see what happens. (Then put it back.)Using the previous code, try to make the following changes:
1. Change the code so that the message still prints five (or ten) times, but the numbers in front count by tens, like so:
Repeater
Type in a message and I'll repeat it 5 times
Message: I didn't do it!
10. I didn't do it!
20. I didn't do it!
30. I didn't do it!
40. I didn't do it!
50. I didn't do it!
2. Change the code so that it asks the person how many times to display the message. Then, print it that many times. Still count by tens.
Repeater
Type in a message and I'll repeat it many times
Message: *Knock, Knock, Knock* Penny?
How many times? 3
10. *Knock, Knock, Knock* Penny?
20. *Knock, Knock, Knock* Penny?
30. *Knock, Knock, Knock* Penny?
Write a program that uses a while loop gets several integers from the user. Sum up all the integers they give you. Stop looping when they enter a 0. Display the total at the end. Here is some sample input/output:
I will add up the numbers you give me.
Number: 6
The total so far is 6
Number: 9
The total so far is 15
Number: -3
The total so far is 12
Number: 2
The total so far is 14
Number: 0
The total is 14.
I will add up the numbers you give me.
Number: 1
The total so far is 1
Number: 2
The total so far is 3
Number: 3
The total so far is 6
Number: 4
The total so far is 10
Number: 5
The total so far is 15
Number: 0
The total is 15