Hide keyboard shortcuts

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 

2 

3import random 

4 

5 

6def guessing_game(): 

7 """Generate a random integer from 1 to 100. 

8 

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) 

13 

14 while True: 

15 user_guess = int(input("What is your guess? ")) 

16 

17 if user_guess == answer: 

18 print(f"Right! The answer is {user_guess}") 

19 break 

20 

21 elif user_guess < answer: 

22 print(f"Your guess of {user_guess} is too low!") 

23 

24 elif user_guess > answer: 

25 print(f"Your guess of {user_guess} is too high!")