Now You Code 4: Guess A Number

Write a program to play the classic "Guess a number" game.

In this game the computer selects a random number between 1 and 10. It's your job to guess the number. Whenever you guess, the computer will give you a hint of higher or lower. This process repeats until you guess the number, after which the computer reports the number of guesses it took you.

For Example:

I'm thinking of a number between 1 and 10...
Your guess: 5
Too low. Guess higher.
Your guess: 7
Too high. Guess lower.
Your guess: 6
You guessed it in 3 tries.

Your loop should continue until your input guess equals the computer generated random number.

How do you make Python generate a random number?


In [20]:
# Sample code which demostrates how to generate a number between 1 and 10
import random 
number = random.randint(1,10)
print(number)


4

Run the cell above a couple of times. Notice how each time you execute the code, it comes up with a different number.

Here's a breakdown of the code

line 1 imports the random module
line 2 randomly selects an integer between 1 and 10
line 3 prints the number

Now that you understand how to generate a random number, try to design then write code for the program. The first step in your program should be to generate the random number.

Step 1: Problem Analysis

Inputs:

Outputs:

Algorithm (Steps in Program):


In [1]:
import random
# Step 2: write code for program

Step 3: Questions

  1. Which loop did you use to solve the problem? Was it a definite or indefinite loop?

Answer:

  1. Modify this program to allow you to guess a number between 1 and 100. How much of your code did you need to change to make this work?

Answer:

  1. This program is a good example of a difficult problem to conceptualize which has a simple solution when you look at actual lines of code. I assume you did not write this in a single try, so explain where you got stuck and describe your approach to overcoming it.

Answer:

Step 4: Reflection

Reflect upon your experience completing this assignment. This should be a personal narrative, in your own voice, and cite specifics relevant to the activity as to help the grader understand how you arrived at the code you submitted. Things to consider touching upon: Elaborate on the process itself. Did your original problem analysis work as designed? How many iterations did you go through before you arrived at the solution? Where did you struggle along the way and how did you overcome it? What did you learn from completing the assignment? What do you need to work on to get better? What was most valuable and least valuable about this exercise? Do you have any suggestions for improvements?

To make a good reflection, you should journal your thoughts, questions and comments while you complete the exercise.

Keep your response to between 100 and 250 words.

--== Write Your Reflection Below Here ==--