Question

In: Computer Science

Write a game where a user has to guess a number from 1 – 6, inclusive....

Write a game where a user has to guess a number from 1 – 6, inclusive.

Your program will generate a random number once (pick a number), and will then prompts the user to guess the number for up to 3 times. If the user enters 3 wrong guesses, the program should be terminated along with the losing message. Once the user has successfully guessed the number, tell the user they win, and tell them how many guesses it took them to guess it right.

Use a loop to check your user input. Use string’s .isnumeric() to check to see if the user has provided you with a valid number before you use a cast to an integer. Your program should not crash if the user gives you bad input. Also if the user’s input wasn’t a digit, do not count it as one of the 3 chances that the user have to guess the number.

To create a random number use:

import random
myRandomNumber = random.randint(1, 6)

Sample game:

I've picked a number between 1 and 6, can you guess it? 1
Nope, it's not 1
Your guess is too low
I've picked a number between 1 and 6, can you guess it? 2
Nope, it's not 2
Your guess is too low
I've picked a number between 1 and 6, can you guess it? 4
Nope, it's not 4
Your guess is too low
You lost :'(. The random pick was 5.

And here is another run of the game that user wins:

I've picked a number between 1 and 6, can you guess it? 1
Nope, it's not 1
Your guess is too low
I've picked a number between 1 and 6, can you guess it? 2
You got it!
You won! :). It took you 2 guesses.

And here is an example that the user enters wrong inputs that are not digits:

I've picked a number between 1 and 6, can you guess it? hello
You entered an invalid entry: hello. You must enter a digit. I do not count this for you!
I've picked a number between 1 and 6, can you guess it?     
You entered an invalid entry: . You must enter a digit. I do not count this for you!
I've picked a number between 1 and 6, can you guess it? 5
Nope, it's not 5
Your guess is too 
I've picked a number between 1 and 6, can you guess it? 4
Nope, it's not 4
Your guess is too high
I've picked a number between 1 and 6, can you guess it? 2
You got it!
You won! :). It took you 3 guesses.

Solutions

Expert Solution

Source Code:

Output:

Code in text format (See above images of code for indentation):

#import random module
import random
#count variable
c=0
#generate random number
myRandomNumber=random.randint(1,6)
#iterate loop
while True:
#read guess from user
guess=input("I've picked a number between 1 and 6, can you guess it? ")
#check for digit or not
if(not guess.isnumeric()):
print("You entered a invalid entry: ",guess,". You must enter a digit. I do not count this for you!",sep='')
else:
#increment guesses
c+=1
#convert to int
guess=int(guess)
#if correct guess
if(guess==myRandomNumber):
print("You got it!\nYou won! :). it took you ",c," guesses.",sep='')
break
#if guess higher
elif(guess>myRandomNumber):
print("Nope it's not ",guess)
print("Your guess is too high")
#if guess lower
else:
print("Nope it's not ",guess)
print("Your guess is too low")
#after 3 attempts fail
if(c==3):
print("You lost:'(.The random pick was ",myRandomNumber,".")
break


  

  

  


Related Solutions

Write a Java program that lets the user play a game where they must guess a...
Write a Java program that lets the user play a game where they must guess a number between zero and one hundred (0-100). The program should generate a random number between 0 and 100 and then the user will try to guess the number. The program should help the user guess the number (see details below). The program must allow the user five attempts to guess the number. Further Instruction: (a) Declare a variable diff and assign to it the...
Write a C program that asks the user to guess a number between 1 and 15(1...
Write a C program that asks the user to guess a number between 1 and 15(1 and 15 are included). The user is given three trials. This is what I have so far. /* Nick Chioma COP 3223 - HW_2 */ #include <iostream> #include <time.h> // needed for time function #include <stdlib.h> // needed for srand, rand functions int main () {       int numbertoguess, num, correct, attempts;       srand(time(NULL)); //this initializes the random seed, making a new...
Project 5-3: Guessing Game Create an application that lets a user guess a number between 1...
Project 5-3: Guessing Game Create an application that lets a user guess a number between 1 and 100. Console Welcome to the Guess the Number Game ++++++++++++++++++++++++++++++++++++ I'm thinking of a number from 1 to 100. Try to guess it. Enter number: 50 You got it in 1 tries. Great work! You are a mathematical wizard. Try again? (y/n): y I'm thinking of a number from 1 to 100. Try to guess it. Enter number: 50 Way too high! Guess...
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 with at least 2 functions that play the game of “guess the number”...
Write a program with at least 2 functions that play the game of “guess the number” as follows: Your program chooses the number to be guessed by selecting an integer at random in the range 1 to 1000. The program then displays the following: I have a number between 1 and 1000. Can you guess my number? Please type in your first guess. The player then types the first guess. The program then responds with one of the following: 1.      ...
Create a PYTHON program that lets a user guess a number from 1 to 1,000: -i...
Create a PYTHON program that lets a user guess a number from 1 to 1,000: -i used random.randint(1,1000) -must give hints like (pick a lower/higher number) which i already did -tell the user when he has repeated a number (help) -the game only ends when you guess the number (help) -you loose $50 every failed attempt (done) ****I did it as a while loop -
Write a program to prompt the user to display the following menu: Guess-Number                        Concat-names     &
Write a program to prompt the user to display the following menu: Guess-Number                        Concat-names             Quit If the user selects Guess-number, your program needs to call a user-defined function called int guess-number ( ). Use random number generator to generate a number between 1 – 100. Prompt the user to guess the generated number and print the following messages. Print the guessed number in main (): Guess a number: 76 96: Too large 10 Too small 70 Close Do you...
C++ PLEASE Write a program to prompt the user to display the following menu: Guess-Number                       ...
C++ PLEASE Write a program to prompt the user to display the following menu: Guess-Number                        Concat-names             Quit If the user selects Guess-number, your program needs to call a user-defined function called int guess-number ( ). Use random number generator to generate a number between 1 – 100. Prompt the user to guess the generated number and print the following messages. Print the guessed number in main (): Guess a number: 76 96: Too large 10 Too small 70 Close...
(HTML) Write a script that plays a “guess the number” game as follows: Your program chooses...
(HTML) Write a script that plays a “guess the number” game as follows: Your program chooses the number to be guessed by selecting a random integer in the range 1 to 1000. The script displays the prompt Guess a number between 1 and 1000 next to a text field. The player types a first guess into the text field and clicks a button to submit the guess to the script. If the player's guess is incorrect, your program should display...
Create the Guessing Game Application. The application should receive two integers from the user, namely minimum and maximum and generate a random integer from minimum through maximum, inclusive. It then should give the user five chances to guess the integ
In Python Guessing Game ApplicationCreate the Guessing Game Application. The application should receive two integers from the user, namely minimum and maximum and generate a random integer from minimum through maximum, inclusive. It then should give the user five chances to guess the integer. Each time the user makes a guess, the application should display one of two messages: “Guess higher” or “Guess lower”. If the user guesses the generated number the application should let her/him know and also display the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT