Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
IEC 2015 - Python Programming Week 8 - Guessing game - While Loops Lesson Plan: Now that the students were introduced to numbers, strings, variables, input() statement, print() statement, comparators and ‘if else’ statements in Python, the next programming challenge we took up was to make them design a guessing game. We chose this game because it would be an excellent way to teach them a few different concepts in Python programming. Ultimately, this game would: 1) Generate a random number (random.randint) 2) Ask the user to guess (input) 3) Compare the two numbers and print the result (if, ><=, print) 4) Give the user 3 chances to guess (while) 5) Ask the user if they want to play again, and go back to step 1 (input, while). Guessing game II: While loops In the previous guessing game I, the students used the other comparators (step3) as well as step 2. For this lesson, they included ‘while’ loops (step 4). We asked them to use counters to count up from 0 to number of guesses, as well as do the reverse - start with maximum number of guesses and count down to 0. Worksheet can be found below. IEC 2015 - Python Programming Week 8 - Guessing game - While Loops Worksheet: Below is the code for a guessing game program similar to the one we made in class. There is a loop that keeps track of how many guesses you have left. number = 6 print("I'm thinking of a number from 1 to 10. Enter your guess.") NumberOfGuesses = 4 while NumberOfGuesses > 0: guess = input() guess = int(guess) if guess == number: print("That's right!") break if guess < number: print("Your guess is too low.") if guess > number: print("Your guess is too high.") NumberOfGuesses = NumberOfGuesses - 1 print("Game over!") Answer the following questions: 1) How many guesses do you have? 2) What will print to the screen if you guess 4? 3) Circle the lines of code that tells the program what to do if you guess the number correctly. 4) Draw arrows to show where the loop begins and ends 5) Why do we include the line “NumberOfGuesses = NumberOfGuesses – 1”? What does this do? Bonus: After you guess correctly, there’s a line in the program that says “break”. What do you think this does? (Hint: do you want to keep guessing after you get the number right?) IEC 2015 - Python Programming Week 8 - Guessing game - While Loops Answer key: Below is the code for a guessing game program similar to the one we made in class. There is a loop that keeps track of how many guesses you have left. number = 6 print("I'm thinking of a number from 1 to 10. Enter your guess.") NumberOfGuesses = 4 while NumberOfGuesses > 0: guess = input() guess = int(guess) if guess == number: print("That's right!") break if guess < number: print("Your guess is too low.") if guess > number: print("Your guess is too high.") NumberOfGuesses = NumberOfGuesses - 1 print("Game over!") Answer the following questions: 1) How many guesses do you have? 4 2) What will print to the screen if you guess 4? Your guess is too low. 3) Circle the lines of code that tells the program what to do if you guess the number correctly. 4) Draw arrows to show where the loop begins and ends 5) Why do we include the line “NumberOfGuesses = NumberOfGuesses – 1”? What does this do? This line counts down the number of guesses. So after each guess, we subtract 1 from the number of guesses remaining. Bonus: After you guess correctly, there’s a line in the program that says “break”. What do you think this does? (Hint: do you want to keep guessing after you get the number right?) “break” ends the loop early, so once you guess the number correctly the program stops.