In: Computer Science
Program Created Last Week:
#Program plays
a guessing the number game with the user.
#Importing random number with min number being 1 and max being
10
import random
min_number = 1
max_number = 10
rand = random.randint(min_number, max_number)
#Prints the header, welcoming the user
print("Welcome to the Guess My Number Program!")
while (True):
#While loop, comparing the users guessed number with the random
number.
#If it matches, it will say you guessed it.
guess = eval(input("Please try to guess my number between 1 and
10:"))
if(guess == rand):
print("You guessed it!")
#break will end the loop once the guess is a match.
#Conditions if the guess is too low or too high to keep
guessing.
break
elif(guess < rand):
print("Too low")
else:
print("Too high")
add to the program you created last week. Python Program You will add input validation and a count to show how many guesses the user took before getting the correct number. The pseudocode is below. Be sure to import random at the beginning of your code and use a comment block explaining what your program does
#Random number, loop while true
#ask user for number. Check to see if the value is a number between 1 and 10
#if number is too high or too low, tell user, if they guessed it break out of loop
Display "Welcome to my Guess the number program!"
random mynumber
count=1
while True
try
Display "Guess a number between 1 and 10"
Get guess
while guess<1 or guess>10
Display "Guess a number between 1 and 10"
Get guess
except
Display "numbers only"
continue
if (guess<mynumber)
Display "Too low"
count=count+1
else if (guess>mynumber)
Display "Too high"
count=count+1
else if (guess==mynumber)
Display "You guessed it in "+ count + " attempts"
When you run the program the result should look like the following:
Welcome to my Guess the number program!
Please guess a number between 1 and 10: a
Numbers only!
Please guess a number between 1 and 10: -3
Please guess a number between 1 and 10: 4
Too low
Please guess a number between 1 and 10: 5
Too low
Please guess a number between 1 and 10: 6
You guessed it! It took you 3 attempts
Python code for guessing game :
# Program plays a guessing the number game with the user. # Importing random number with min number being 1 and max being 10 import random min_number = 1 max_number = 10 #set number of guesses =0 numGuesses=0 rand = random.randint(min_number, max_number) # Prints the header, welcoming the user print("Welcome to the Guess My Number Program!") # While loop, comparing the users guessed number with the random number. # If it matches, it will say you guessed it. while (True): #use try-block try: guess = eval(input("Please try to guess my number between 1 and 10:")) #check if guess is less than 0, then continue to begining of loop if(guess<0): continue; elif (guess == rand): #increment the guess count by 1 numGuesses=numGuesses+1 print("You guessed it!It took you ", numGuesses,"attempts") # break will end the loop once the guess is a match. # Conditions if the guess is too low or too high to keep guessing. break elif (guess < rand): # increment the guess count by 1 numGuesses = numGuesses + 1 print("Too low") else: # increment the guess count by 1 numGuesses = numGuesses + 1 print("Too high") except: #print exception print("Numbers only!")
---------------------------------------------------------------------------------------------------------------
Screenshots of the python program :
-------------------------------------------------------------------------------------------------------
Sample Output:
Welcome to the Guess My Number Program!
Please try to guess my number between 1 and 10:a
Numbers only!
Please try to guess my number between 1 and 10:-5
Please try to guess my number between 1 and 10:5
Too low
Please try to guess my number between 1 and 10:6
Too low
Please try to guess my number between 1 and 10:7
Too low
Please try to guess my number between 1 and 10:8
Too low
Please try to guess my number between 1 and 10:9
Too low
Please try to guess my number between 1 and 10:10
You guessed it!It took you 6 attempts