In Python, I need help with this assignment: Modify the Guess My Number Chapter
ID: 643653 • Letter: I
Question
In Python, I need help with this assignment:
Modify the Guess My Number Chapter Project from Chapter 3 by reusing the function ask_number() from the Tic-Tac-Toe Chapter Project from this chapter. Replace the code in Guess My Number that asks the player for a guess with a call to ask_number( ).
Here is the original Guess My Number game:
import random
guessesTaken = 0
print("Hello what is your name?")
name = input()
number = random.randint(1,20)
print("Well",name,",I am thinking of a number between 1 and 20.")
while guessesTaken < 6:
print("Take a guess")
guess = input()
guess = int(guess)
guessesTaken = guessesTaken + 1
if guess < number:
print("your guess is to low")
if guess > number:
print("Your guess is to high")
if guess == number:
break
if guess == number:
guessesTaken = str(guessesTaken)
print("Good job",name,"You guessed my number in",guessesTaken,"guesses!")
if guess != number:
number = str(number)
print("Nope, the number I was thinking of was",number)
Here is the ask_number function:
This function asks for a number within a range. It receives a question, a low number, and a high number. It returns a number within the range specified.
def ask_number( question, low, high):
""" Ask for a number within a range."""
response = None
while response not in range( low, high):
response = int( input( question))
return response
Explanation / Answer
import random
def ask_number( question, low, high):
""" Ask for a number within a range."""
response = None
while response not in range( low, high):
response = int( input( question))
return response
guessesTaken = 0
print("Hello what is your name?")
name = input()
number = random.randint(1,20)
print("Well",name,",I am thinking of a number between 1 and 20.")
while guessesTaken < 6:
guess = ask_number("Take a guess: ", 1, 20)
guessesTaken = guessesTaken + 1
if guess < number:
print("your guess is to low")
if guess > number:
print("Your guess is to high")
if guess == number:
break
if guess == number:
guessesTaken = str(guessesTaken)
print("Good job",name,"You guessed my number in",guessesTaken,"guesses!")
if guess != number:
number = str(number)
print("Nope, the number I was thinking of was",number)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.