Loops

Loops

 Requirements

1.  Write a program that finds the SUM of the numbers from one to one hundred, by adding them all up (don’t use a formula – even if you know one).  Use a loop – don’t just write 1 + 2 + 3 + …  .  Your program should print out: “The sum of the numbers from one to one hundred is….  This is one_hundred

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.  This is validation.

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.  This is number_game2

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

Finally…

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.  This is number_game3

Resources

For Loops from Mark Sobkowicz on Vimeo.

While Loops from Mark Sobkowicz on Vimeo.

Number Game from Mark Sobkowicz on Vimeo.