| |
- Though
for loops are the preferred way to
write loops when you know in advance how many times you want to
loop, such loops can also be written with while loops. Write
a program using a while loop which will print out
the numbers from 1 to 50 and, when done, print out the sum of those
numbers as well.
- Write a program which prompts the user for a positive number
and tells them whether it is odd or even. If they do not enter a positive number,
repeatedly prompt them until they do.
- Write a program which generates a random number between
1 and 20 and asks the user to guess it. If they guess right, they are
complimented and the program ends. If they guess wrong, tell them
whether they are too high or too low and go back and get another guess.
- Write a program based on the last one, but in which the user is limited to
at most 5 guesses. (It may help you to first modify the program so that it simply
keeps track of how many guesses they have made and tells them at each guess
what guess number they are on.)
- Write a program that plays the guessing side of the guessing game. Tell the user to
think of a number from one to twenty. After the program guesses a random number in that range, ask the user if it is correct. Allow for three responses: "yes","higher", or "lower".
If you get one of the latter two responses, have the program guess again, but modifying
the range of the guess appropriately. Repeat until the program guesses correctly, or loses for
using up five guesses.
|