Question
Unit 5: Accumulating Totals in a Loop in Python Summary: In this lab, you will make additions to a Python program provided. The program is
Unit 5: Accumulating Totals in a Loop in Python
Summary:
In this lab, you will make additions to a Python program provided. The program is a guessing game. A random number between 1 and 10 is generated in the program. The user enters a number between 1 and 10, trying to guess the correct number.
If the user guesses correctly, the program congratulates the user, and then the loop that controls guessing numbers exits; otherwise the program asks the user if he or she wants to guess again. If the user enters "Y", he or she can guess again. If the user enters "N", the loop exits. You can see that the "Y" or "N" is the sentinel value that controls the loop. Note that the entire program has been written for you.
You need to add code that validates correct input, which is "Y" or "N" when the user is asked if he or she wants to guess a number, and a number in the range of 1 through 10 when the user is asked to guess a number.
Instructions:
Make sure the file GuessNumber.py is selected and open.
Write loops that validate input at all places in the code where the user is asked to provide input. Comments have been included in the code to help you identify where these loops should be written.
Execute the program. See if you can guess the randomly generated number. Execute the program several times to see if the random number changes. Also, test the program to verify that incorrect input is handled correctly. On your best attempt, how many guesses did you have to take to guess the correct number?
Assignment:
"""
GuessNumber.py - This program allows a user to guess a number
between 1 and 10.
Input: User guesses numbers until they get it right.
Output: Tells users if they are right or wrong.
"""
import random
number = random.randint(1, 10)
# Prime the loop.
keepGoing = input("Do you want to guess a number? Enter Y or N ")
# Validate input.
while keepGoing != 'Y' and keepGoing != 'Y':
keepGoing = input("Please enter valid input. Do you want to guess again? Enter Y or N ")
# Enter loop if they want to play.
while keepGoing == "Y":
# Get user's guess.
stringNumber = input("I'm thinking of a number. . Try to guess by entering a number between 1 and 10 ")
userNumber = int(stringNumber)
# Validate input.
while userNumber < 1 or userNumber > 10:
stringNumber = input("Input invalid. Try to guess by entering a number between 1 and 10 ")
userNumber = int(stringNumber)
# Test to see if the user guessed correctly.
if userNumber == number:
keepGoing = "N"
print("You are a genius. That's correct!")
else:
keepGoing = input("That's not correct. Do you want to guess again? Enter Y or N ")
# Validate input.
while keepGoing != 'Y' and keepGoing != 'Y':
keepGoing = input("Please enter valid input. Do you want to guess again? Enter Y or N ")
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started