Answer to Question #313648 in Python for ABCD

Question #313648

Write an application that plays "guess the number" as follows: Your program chooses the number to be guessed by selecting a random integer in the range 1 to 1000. The application displays the prompt Guess a number between 1 and 1000. The player inputs a first guess. If the player's guess is incorrect, your program should display Too high. Try again. or Too low. Try again. To help the player "zero in" on the correct answer, the program should prompt the user for the next guess. When the user enters the correct answer, display Congratulations. You guessed the number!, and allow the user to choose whether to play again.


Also, count the number of guesses the player makes. If the number is 10 or fewer, display Either you know the secret or you got lucky! If the player guesses the number in 10 tries, display Aha! You know the secret! If the player makes more than 10 guesses, display You should be able to do better!


1
Expert's answer
2022-03-18T03:19:18-0400
from random import randint

def guess_the_number():
    count = 0
    number = randint(1, 1000)
    print("I made a number, try to guess it: ")
    attempt = 0
    while(attempt != number):
        try:
            attempt = int(input(">> "))
        except ValueError:
            print("Please enter a number!")
            continue
        
        if(attempt > number):
            print("Too high. Try again.")
            count += 1
        if(attempt < number):
            print("Too low. Try again.")
            count += 1
        if(attempt == number):
            print("You guessed the number!")
            if(count < 10):
                print("Either you know the secret or you got lucky!")
            if(count == 10):
                print("Aha! You know the secret!")
            if(count > 10):
                print("You should be able to do better!")

while(1):
    answer = input("Do you want to play? (Y/N): ")
    if(answer == "Y"):
        guess_the_number()
    if(answer == "N"):
        break

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS