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!
Trending nowThis is a popular solution!
Step by stepSolved in 4 steps with 2 images
- Q1. FizzBuzz problem:- Write a program which return "fizz" if the number is a multiplier of 3, return "buzz" if its multiplier of 5 and return "fizzbuzz" if the number is divisible by both 3 and 5. If the number is not divisible by either 3 or 5 then it should just return the number itself? .arrow_forwardOverview In this assignment, you will gain more practice with designing a program. Specifically, you will create pseudocode for a higher/lower game. This will give you practice designing a more complex program and allow you to see more of the benefits that designing before coding can offer. The higher/lower game will combine different programming constructs that you have been learning about, such as input and output, decision branching, and a loop. Higher/Lower Game DescriptionYour friend Maria has come to you and said that she has been playing the higher/lower game with her three-year-old daughter Bella. Maria tells Bella that she is thinking of a number between 1 and 10, and then Bella tries to guess the number. When Bella guesses a number, Maria tells her whether the number she is thinking of is higher or lower or if Bella guessed it. The game continues until Bella guesses the right number. As much as Maria likes playing the game with Bella, Bella is very excited to play the game…arrow_forwardJava - Name Formatarrow_forward
- assembly language please with comment and screen shot of the out putarrow_forwardPython please: If the score is between 90 and 100, the grade is A, and the teacher comment is 'Exceptional work' for 100 only 'Excellent work' for anything other than 100 If the score is between 80 and 89, the grade is B, and the teacher comment is 'Very Good, can do better' If the score is between 70 and 79, the grade is C, and the teacher comment is 'Good, but need to work harder' If the score is between 60 and 69, the grade is D, and there are no teacher comments. If the score is below 60, the grade is F, and the teacher comment is 'Fail, need to appear for a retest' For example if the input (student test score) is 100 the output is The Student grade is A and the teacher comment is 'Exceptional work'arrow_forwardJavaarrow_forward
- Computer Networking: A Top-Down Approach (7th Edi...Computer EngineeringISBN:9780133594140Author:James Kurose, Keith RossPublisher:PEARSONComputer Organization and Design MIPS Edition, Fi...Computer EngineeringISBN:9780124077263Author:David A. Patterson, John L. HennessyPublisher:Elsevier ScienceNetwork+ Guide to Networks (MindTap Course List)Computer EngineeringISBN:9781337569330Author:Jill West, Tamara Dean, Jean AndrewsPublisher:Cengage Learning
- Concepts of Database ManagementComputer EngineeringISBN:9781337093422Author:Joy L. Starks, Philip J. Pratt, Mary Z. LastPublisher:Cengage LearningPrelude to ProgrammingComputer EngineeringISBN:9780133750423Author:VENIT, StewartPublisher:Pearson EducationSc Business Data Communications and Networking, T...Computer EngineeringISBN:9781119368830Author:FITZGERALDPublisher:WILEY