Question

In: Computer Science

Create a Guess the Number game. Python randomly chooses a secret four-digit number (with no repeating...

Create a Guess the Number game.

  • Python randomly chooses a secret four-digit number (with no repeating digits) and asks the user to guess it.
  • The user has up to ten attempts to guess the number. After each guess, Python gives the user two clues (until the user either guesses the number correctly or runs out of attempts):
    • The number of digit(s) in the user’s guess that is (are) both correct and in the right position.
    • The number of digit(s) in the user’s guess that is (are) correct but in the wrong position.
      • For example, say the secret number is 0123.
        • If the user’s guess is 4567, the clues will be:
          • Right digit and position: 0
          • Right digit but wrong position: 0
        • If the user’s guess is 1023, the clues will be:
          • Right digit and position: 2
          • Right digit but wrong position: 2
  • If the user guesses the correct number within his/her ten attempts, the game will finish.
  • If the user fails to guess the correct number within his/her ten attempts, the number will be shown and the game will finish.
  • When the game finishes (i.e., whether the user guesses the correct number or fails to do so in ten attempts), Python asks the user whether he/she wants to continue playing.
    • If the user enters “Y” he/she will play another round. If the user enters “N” the game will end
  • PLEASE USE LOW-LEVEL PYTHON LANGUAGE SUCH AS WHILE LOOP OR IF ELSE ARGUMENTS, TRY NOT TO MAKE IT TOO ADVANCED

Solutions

Expert Solution

Answer:

Here is the python code as per your requirement

Raw code:

#Code to generate secret number with four unique digits

import random

#user to ask if he want's to paly again after 10

tryAgain='Y'

#While loop

while (tryAgain!='N'):

#digits

digts='0123456789'

#secret string

secret=''

#iterating while loop till the string legth is 4

while(len(secret)<4):

#get the random number from digts string

num=random.choice(digts)

#if that digt is already in secret string don't do anything

if num in secret:

continue

#or else concatnate the string to secret

else:

secret=secret+num

#count initialized to 0

count=0

#to give chances 10 attempts

while(count<10):

#declaring variables to conoutn right postions at right index and right values and not right indexes

right_pos_num_count=0

right_num_nopos_count=0

#asking the user to enter guess

guess=input("Guess: ")

#iterate over the guess

for i in range(len(guess)):

#iterate over the secretn

for j in range(len(secret)):

#checking the position and index

if i==j and guess[i]==secret[j]:

#if both are same increment the count

right_pos_num_count+=1

#if positions are same and indexes aren't

if guess[i]==secret[j] and i!=j:

right_num_nopos_count+=1

#print the hints

print('Right digit and position: ',right_pos_num_count)

print('Right digit but wrong position: ',

right_num_nopos_count)

#if user guess right

if right_pos_num_count==4:

#print this and break the loop of 10

print('Congratulations you Guessed it right!')

break

#else incremnt the count of attempts

else:

count+=1

#ask the user if he wants to play again

tryAgain=input('wants to continue playing? (Y | N): ').upper()

Editor:

output:

Hope this helps you! If you still have any doubts or queries please feel free to comment in the comment section.

"Please refer to the screenshot of the code to understand the indentation of the code".

Thank you! Do upvote.


Related Solutions

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!...
(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...
Part1. Create a number guessing game in Python. Randomly generate a number from 1 to 10....
Part1. Create a number guessing game in Python. Randomly generate a number from 1 to 10. Have the user guess the number. If it is too high, tell the user to guess lower - it it is too low, tell the user to guess higher. Continue until she guesses the correct number. - Part 2. Allow the user to play a new game after they have guessed correctly.   Part 3. Ask for a different name and keep track of how...
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...
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 -
You need to create a four-digit password using 0-9. How many passwords are possible, without repeating...
You need to create a four-digit password using 0-9. How many passwords are possible, without repeating a number, if a password must not start with a 0?
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...
A thief steals an ATM card and must randomly guess thecorrect five​-digit pin code...
2. A thief steals an ATM card and must randomly guess the correct five-digit pin code from a 5​-key keypad. Repetition of digits is allowed. What is the probability of a correct guess on the first​ try? The number of possible codes is___. the probability that correct code is given on the first try is?   3. If you know the names of the remaining seven students in the spelling​ bee, what is the probability of randomly selecting an order and...
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.      ...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT