In: Computer Science
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.
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