I have to use the function ask_number() so that is ask the player for a guess with a call to ask_number()

here is my code but it automatically guess the number and i dont know if i did it right

import random

print "\tWelcome to guess my number!"
print "nI'm thinking of a number bewtween 1 and 100."
print "try to guess it in as few attempts as possible.\n"

tries = 1
the_number= random.randrange(100) +1

def ask_number(question, low, high):
    """Ask a yes or no question"""
    response = None
    while response not in range(low, high):
        response = int(raw_input(question))
        return response


    guess = int(raw_input("take a guess: "))
    tries +=1


print "you guess it! the number was", the_number
print "and it only took you", tries, "tries!\n"


raw_input("\n\nPress the enter key to exit.")

Recommended Answers

All 4 Replies

Lines 18 and 19 should not be indented, and should use a call to ask_number(). Also there should be a loop somewhere.

okay i can now put in my own number but doesnt tell me if im either too high or low and it suppose to let me guess 5 times and its only letting me do it once.
my new code

import random

print "\tWelcome to guess my number!"
print "nI'm thinking of a number bewtween 1 and 100."
print "try to guess it in as few attempts as possible.\n"
guess = int(raw_input("take a guess: "))

tries = 5
the_number= random.randrange(100) +1

def ask_number(question, low, high):
    """Ask a yes or no question"""
    response = None
    while response not in range(low, high):
        response = int(raw_input(question))
        return response


   
    tries +=1


print "you guess it! the number was", the_number
print "and it only took you", tries, "tries!\n"


raw_input("\n\nPress the enter key to exit.")

Lines 18 and 19 should not be indented, and should use a call to ask_number(). Also there should be a loop somewhere.

i fixed the indention after i posted the first time, but i dont know where i should put the ask_number() and this loop i thought i already had the loop and ask_number()

i made another code change now it wont let me run my code and i dont know why. if someone show me where the mistake is and how to fixz it thank you

import random

secretNum = random.randrange(100)+1
      
tries = 0

while tries < 5:
    try:
        def ask_number(question, high, low):
            """Ask a yes or no question"""
            response = None
            while response not in range(low, high):
                response = int(raw_input(question))
                return response              
                                                                       
    except ValueError:
        print('Please enter a number')

if guess != secretNum:
    print('Sorry you ran out of guesses. The correct number was'), secretNum
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.