Python gussing game Have you ever played, “I’m thinking of a number between….” ?
ID: 3689043 • Letter: P
Question
Python gussing game
Have you ever played, “I’m thinking of a number between….”? In this game, the computer selects a random number that the user (player) has to guess. The computer gives a single hint after each guess, but cannot give the same type of hint every time. For example, the computer could hint:
The number is higher/lower
The number is not a multiple of…
Plus one additional type of clue (get creative! No submission should have the same type of third hint as any other.)
Upon completion of the game (i.e., the user guessing the number), the user enters their name, and their score is stored in a file. The game displays the leader board with the top ten winners. This leader board will need to be stored as a file in the same folder as your python program file.
Explanation / Answer
Answer for Question:
This below python code will wor as specified in problem statement.
1. User can guess number i.e generated by computer for randome
number
2. Computer will give some hint's specified in problem..
import random
def main(low=1, high=100):
"""Guess My Number
The computer picks a random number between low and high
The player tries to guess it and the computer lets
the player know if the guess is too high, too low
or right on the money
"""
print("Welcome to 'Guess My Number'!")
print("I'm thinking of a number between {} and {}.".format(low, high))
print("Try to guess it in as few attempts as possible.")
the_number = random.randint(low, high)
tries = 0
while True:
guess = ask_number("Enter your guess:", low, high)
if guess == the_number:
print("You're right on the money!")
break
elif guess > the_number:
print("Too high!")
else:
print("Too low!")
tries += 1
print("You guessed it! The number was {}.".format(the_number))
print("And it only took you {} tries!".format(tries))
input("Press the enter key to exit.")
def ask_number(question, low, high):
"""Get the user to input a number in the appropriate range."""
while True:
try:
response = int(input(question))
except ValueError:
print "Not an integer"
else:
if response in range(low, high+1):
return response
print "Out of range"
if __name__ == "__main__":
while True:
main()
again = input("Play again (y/n)? ").lower()
if again not in {"y", "yes"}:
break
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.