Coverage for e01_guessing_game.py : 100%

Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1#!/usr/bin/env python3
3import random
6def guessing_game():
7 """Generate a random integer from 1 to 100.
9Ask the user repeatedly to guess the number.
10Until they guess correctly, tell them to guess higher or lower.
11"""
12 answer = random.randint(0, 100)
14 while True:
15 user_guess = int(input("What is your guess? "))
17 if user_guess == answer:
18 print(f"Right! The answer is {user_guess}")
19 break
21 elif user_guess < answer:
22 print(f"Your guess of {user_guess} is too low!")
24 elif user_guess > answer:
25 print(f"Your guess of {user_guess} is too high!")