Guessing Game¶

A guessing game where the Computer thinks of a random number between 1 and 100 and the User tries to guess the number!

In [ ]:
import random
In [ ]:
secret = random.randint(1,100)
print(secret) # we will delete this statement after we have completed coding this game
print("I am thinking of a number...")
while True:
    guess = int(input("What is your guess? "))
    if guess == secret:
        print("That's right!")
        break
1
I am thinking of a number...
What is your guess?  2
What is your guess?  3
What is your guess?  1
That's right!

The game is not interesting unless the computer gives the user a hint!

In [ ]:
secret = random.randint(0,100)
print(secret)
print("I am thinking of a number...")
while True:
    guess = int(input("What is your guess? "))
    if guess == secret:
        print("That's right!")
        break
    elif guess < secret:
        print("My number is bigger")
    else:
        print("My number is smaller")
65
I am thinking of a number...
What is your guess?  a23
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-4-69b608671dc2> in <module>
      3 print("I am thinking of a number...")
      4 while True:
----> 5     guess = int(input("What is your guess? "))
      6     if guess == secret:
      7         print("That's right!")

ValueError: invalid literal for int() with base 10: 'a23'
In [ ]:
secret = random.randint(0,100)
print(secret)
print("I am thinking of a number...")
while True:
    try:
        guess = int(input("What is your guess? "))
    except ValueError:
        print("Please input a number.")
        continue
    if guess == secret:
        print("That's right!")
        break
    elif guess < secret:
        print("My number is bigger")
    else:
        print("My number is smaller")
83
I am thinking of a number...
What is your guess?  a23
Please input a number.
What is your guess?  23
My number is bigger
What is your guess?  83
That's right!

what if user gives a number outside the range, 1..100?

In [ ]:
secret = random.randint(0,100)
print(secret)
print("I am thinking of a number...")
while True:
    try:
        guess = int(input("What is your guess? "))
    except ValueError:
        print("Please input a number.")
        continue
    if guess < 1 or guess > 100:
        print("Please input a number between {} and {}".format(1,100))
        continue
    if guess == secret:
        print("That's right!")
        break
    elif guess < secret:
        print("My number is bigger")
    else:
        print("My number is smaller")
6
I am thinking of a number...
What is your guess?  9
My number is smaller
What is your guess?  -23
Please input a number between 1 and 100
What is your guess?  101
Please input a number between 1 and 100
What is your guess?  6
That's right!

What if we want to change the range in this game?

In [ ]:
minguess = 1
maxguess = 100
secret = random.randint(minguess,maxguess)
print(secret)
print("I am thinking of a number...")
while True:
    try:
        guess = int(input("What is your guess? "))
    except ValueError:
        print("Please input a number.")
        continue
    if guess < minguess or guess > maxguess:
        print("Please input a number between {} and {}".format(minguess,maxguess))
        continue
    if guess == secret:
        print("That's right!")
        break
    elif guess < secret:
        print("My number is bigger")
    else:
        print("My number is smaller")

Let us keep track of number of guesses¶

In [ ]:
### minguess = 1
maxguess = 100
secret = random.randint(minguess,maxguess)
print(secret)
print("I am thinking of a number...")
num_guesses = 0
while True:
    try:
        guess = int(input("What is your guess? "))
    except ValueError:
        print("Please input a number.")
        continue
    if guess < minguess or guess > maxguess:
        print("Please input a number between {} and {}".format(minguess,maxguess))
        continue
    num_guesses = num_guesses + 1
    if guess == secret:
        print("That's right!")
        break
    elif guess < secret:
        print("My number is bigger")
    else:
        print("My number is smaller")
print("You needed {} guesses".format(num_guesses))
82
I am thinking of a number...
What is your guess?  22
My number is bigger
What is your guess?  33
My number is bigger
What is your guess?  44
My number is bigger
What is your guess?  82
That's right!
You needed 4 guesses
In [ ]: