Question

In: Computer Science

Program Created Last Week: #Program plays a guessing the number game with the user. #Importing random...

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

Solutions

Expert Solution

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


Related Solutions

C++ The program implements the Number Guessing Game. However, in that program, the user is given...
C++ The program implements the Number Guessing Game. However, in that program, the user is given as many tries as needed to guess the correct number. Rewrite the program so that the user has no more than five tries to guess the correct number. Your program should print an appropriate message, such as “You win!” or “You lose!”.
Random Number Guessing Game Write a program in C++ that generates a random number between 1...
Random Number Guessing Game Write a program in C++ that generates a random number between 1 and 100 and asks the user to guess what the number is. If the user’s guess is higher than the random number, the program should display “Too high. Try again.” If the user’s guess is lower than the random number, the program should display “Too low. Try again.” The program should use a loop that repeats until the user correctly guesses the random number....
Random Number Guessing Game C++. Write a program that generates a random number between 5 and...
Random Number Guessing Game C++. Write a program that generates a random number between 5 and 20 and asks the user to guess what the number is. If the user’s guess is higher than the random number, the program should display Too high. Try again. If the user’s guess is lower than the random number, the program should display Too low, Try again. The program should use a loop that repeats while keeping a count of the number of guesses...
Random Number Guessing Game (python) *use comments Write a program guess.py that generates a random number...
Random Number Guessing Game (python) *use comments Write a program guess.py that generates a random number in the range of 1 through 20, and asks the user to guess what the number is. If the user's guess is higher than the random number, the program should display "Too high, try again." If the user's guess is lower than the random number, the program should display "Too low, try again." If the user guesses the number, the application should congratulate the...
Write a Java program that implements the Number Guessing Game: 1. First generate a random number...
Write a Java program that implements the Number Guessing Game: 1. First generate a random number (int) between 0 and 100, call it N 2. Read user input (a guess) 3. check the number, if it's smaller than N, output "The number is larger than that" 4. If the input is larger than N, output "The number is smaller than that" 5. If the input is equal to N, output " You got it!", and exit 6. Repeat until the...
guessing game in Java. It will have a guess input used for guessing the random number...
guessing game in Java. It will have a guess input used for guessing the random number that is generated from 1 - 100. When the user makes a guess it will tell them if the number is lower or higher than the guess. There is also a choice to give up which then reveals the correct number. The last choice will be new game which resets the random number. Last, the program should keep counts of tries. When the user...
The goal of the game (program) is let a user input a random number in the...
The goal of the game (program) is let a user input a random number in the range of 0-10, and then give the user feedback as to whether this number is the same, bigger or smaller than the one the computer generated. Given this, the second task is to refine the program we just created so that the program compares the user's input with the secret number to If the user's input is equal to the number, print out the...
Write a program in Matlab where the program plays a hot and cold game. The user...
Write a program in Matlab where the program plays a hot and cold game. The user answers two inputs: x=input('what is the x location of the object?') y=input('what is the y location of the object?') You write the program so that the computer plays the game. Pick a starting point. Program Calculates the distance to the object. Program picks another point, calculates the distance to the object. Program knows its at the right spot when the distance is less than...
Number guessing Game (20 Marks) Write a C program that implements the “guess my number” game....
Number guessing Game Write a C program that implements the “guess my number” game. The computer chooses a random number using the following random generator function srand(time(NULL)); int r = rand() % 100 + 1; that creates a random number between 1 and 100 and puts it in the variable r. (Note that you have to include <time.h>) Then it asks the user to make a guess. Each time the user makes a guess, the program tells the user if...
Write a program using Python that allows the user to play a guessing game (please provide...
Write a program using Python that allows the user to play a guessing game (please provide a picture of code so it is easier to read). The game will choose a "secret number", a positive integer less than 10000. The user has 10 tries to guess the number. Requirements: Normally, we would have the program select a random number as the "secret number". However, for the purpose of testing your program (as well as grading it), simply use an assignment...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT