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...
PYTHON Exercise 5. Guess the number 1. Develop a “Guess the number” game. Write a program...
PYTHON Exercise 5. Guess the number 1. Develop a “Guess the number” game. Write a program that comes up with a random number and the player has to guess it. The program output can be like this: I am thinking of a number between 1 and 20. Take a guess. 10 Your guess is too low. Take a guess. 15 Your guess is too low. Take a guess. 17 Your guess is too high. Take a guess. 16 Good job!...
We are going to develop a guessing game where the user has to guess a secret...
We are going to develop a guessing game where the user has to guess a secret number. We will start from a simple task and refine our code step by step. Task is to develop a program that Generates a random integer number in the range of [1-10] as the secret number Asks for user to input an integer in the range of [1-10] Print out the secret number and a user's input. Steps Please download this sample code, using...
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.      ...
In Java: Write a program that generates a random number and asks the user to guess...
In Java: Write a program that generates a random number and asks the user to guess the number and keeps track of how many guesses it took If the user input is negative or zero then the loop must stop reading further inputs and display how many guesses they used If they guess the correct number display a message telling them they got it and exit the program If they guess the wrong number (but still a legal guess) you...
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 -
17. Write a program that generates a random number and asks the user to guess what...
17. Write a program that generates a random number 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. 18. Enhance the program that you wrote for Programming...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT