Loops

 Requirements

 

1.  Write a program that prints the numbers from one to one hundred, each on its own line.  Use a loop – don’t write a one hundred line program!

save this as one_hundred.py.

Use a loop of the type for i in range( )

2.  Write a program that asks the user for a number between one and ten, and then rejects any answer that is not between one and ten!  But then it asks again, and it does this until the user puts in something that is between one and ten.  When you do put a number between one and ten the program thanks you and quits.  Save this as validation.py.

To do this, use a while True loop and a break statement.

3.  Write a number game program that picks a number between one and one hundred.  Then it gives the user as many guesses as the user needs to get it right!  Each time, the program tells the user whether their guess is correct, too high, or too low.  Write it with a loop, so that no matter how many guesses the user needs, they can keep playing.  Save this as  number_game2.py.

This one should also use a while True loop and a break statement.

And more…

Make the number game better.  Have it count how many guesses the user takes.  Or have it ask the player’s name and use it throughout the game.  Or have it ask if you want to play again once the game is over.  Or have it tell you when you’re really close.  You choose.  Make it a fun game.  Save your best version as number_game3.py.

 

 

Resources

For Loops from Mark Sobkowicz on Vimeo.

While Loops from Mark Sobkowicz on Vimeo.

Number Game from Mark Sobkowicz on Vimeo.