Problem John is developing a python program that comes up with a number and allows the user to guess that number. The game allows the user to input their guess, which afterward, the game will tell the user one of three things: (1) the guessed number is too high, (2) the guessed number is too low, or (3) the guessed number is correct. If #3 occurs, then the game ends. Learning Objectives Practice conditionally executing code. Practice reading portions of code already written for you. Understand the flow of a program. Understand how and when the program will be executed. Template from random import randint def guess(user_guess, correct_num): # If the user guesses wrong, tell them if their # guess is too low or too high, then return False. # Fix the XXXXX's in the template below to get it to work correctly if XXXXX: # TODO: Print the output and return False elif XXXXX: # TODO: Print the output and return False else: # TODO: If the user guesses correctly, congratulate them # and return True if __name__ == "__main__": num = randint(-50, 50) while True: user_guess = int(input("Enter your guess: ")) if guess(user_guess, num) == True: break Assignment Write the guess(user_num, correct_num) code. In the guess() code, you need a series of if statements to test three possibilities: (1) the player guessed too high, (2) the player guessed too low, or (3) the player guessed correctly. Only #3 will end the game. The number the user actually guessed is in the variable user_num. The number the game randomly generated is in the variable correct_num. The guess() code needs to do three things: (1) determine if the guess is too high, too low, or correct, (2) tell the player how they guessed (too high, too low, correctly), (3) return True if the user got the number or return False otherwise. #3 is required because we stop asking for guesses when your guess() function returns True. Otherwise, we keep looping. If the player's guess is too high, output "Your guess is too high!" and return False. If the player's guess is too low, output "Your guess is too low!" and return False. If the player's guess is correct, output "YOU GOT IT!" and return True. Testing For all input, know that the number is random, so you might get different results.Here's a sample interaction where the random number is -16. Enter your guess: 5 Your guess is too high! Enter your guess: -2 Your guess is too high! Enter your guess: -10 Your guess is too high! Enter your guess: -25 Your guess is too low! Enter your guess: -15 Your guess is too high! Enter your guess: -19 Your guess is too low! Enter your guess: -17 Your guess is too low! Enter your guess: -16 YOU GOT IT!

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question

Problem

John is developing a python program that comes up with a number and allows the user to guess that number. The game allows the user to input their guess, which afterward, the game will tell the user one of three things: (1) the guessed number is too high, (2) the guessed number is too low, or (3) the guessed number is correct. If #3 occurs, then the game ends.

Learning Objectives

  • Practice conditionally executing code.
  • Practice reading portions of code already written for you.
  • Understand the flow of a program.
  • Understand how and when the program will be executed.

Template

from random import randint def guess(user_guess, correct_num): # If the user guesses wrong, tell them if their # guess is too low or too high, then return False.
# Fix the XXXXX's in the template below to get it to work correctly
if XXXXX:
# TODO: Print the output and return False elif XXXXX:
# TODO: Print the output and return False
else:
# TODO: If the user guesses correctly, congratulate them # and return True if __name__ == "__main__": num = randint(-50, 50) while True: user_guess = int(input("Enter your guess: ")) if guess(user_guess, num) == True: break

Assignment

Write the guess(user_num, correct_num) code.

  • In the guess() code, you need a series of if statements to test three possibilities: (1) the player guessed too high, (2) the player guessed too low, or (3) the player guessed correctly. Only #3 will end the game.
  • The number the user actually guessed is in the variable user_num. The number the game randomly generated is in the variable correct_num.
  • The guess() code needs to do three things: (1) determine if the guess is too high, too low, or correct, (2) tell the player how they guessed (too high, too low, correctly), (3) return True if the user got the number or return False otherwise. #3 is required because we stop asking for guesses when your guess() function returns True. Otherwise, we keep looping.
  • If the player's guess is too high, output "Your guess is too high!" and return False.
  • If the player's guess is too low, output "Your guess is too low!" and return False.
  • If the player's guess is correct, output "YOU GOT IT!" and return True.

Testing

For all input, know that the number is random, so you might get different results.Here's a sample interaction where the random number is -16.

Enter your guess: 5 Your guess is too high! Enter your guess: -2 Your guess is too high! Enter your guess: -10 Your guess is too high! Enter your guess: -25 Your guess is too low! Enter your guess: -15 Your guess is too high! Enter your guess: -19 Your guess is too low! Enter your guess: -17 Your guess is too low! Enter your guess: -16 YOU GOT IT!

Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 4 steps with 2 images

Blurred answer
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY