i need the code to ask the play for a guess with a call to ask_number()

here is my code i have a problem in that it won't run can somebody offer me some help? thanks

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

You have the function inside your while loop. Functions must always be called so they will execute their code. To call it you need to type the name of the function and include any variables it needs within the parenthesis. Like so: ask_number("Type a number: ", 1, 100) In this case this will not work because Python begins the while() loop, which defines your function, before it reaches your function call. Also, placing the call before the while() loop will not work because your function is not defined yet.

You simply need to rearrange your code so the function is outside of your loop, and add the function call at the end.

import random

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

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

ask_number("Type a number: ", 1, 100)

You have still things left to fix Wildbamaboy only gave you start. One thing I for example do not understand is your docstring. Make some test cases for your code.

You have the function inside your while loop. Functions must always be called so they will execute their code. To call it you need to type the name of the function and include any variables it needs within the parenthesis. Like so: ask_number("Type a number: ", 1, 100) In this case this will not work because Python begins the while() loop, which defines your function, before it reaches your function call. Also, placing the call before the while() loop will not work because your function is not defined yet.

You simply need to rearrange your code so the function is outside of your loop, and add the function call at the end.

import random

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

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

ask_number("Type a number: ", 1, 100)

thanks for the help but now it wont let me do my 5 tries and tell me if im high or low.

i fixed some of my code looked over what i had to do and figured out that i was adding stuff i didnt need. but i still need to figure out how i can make it tell me if i need it to be higher or lower

here is my code

# Guess My Number
#
# The computer picks a random number between 1 and 100
# 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

import random  

print "\tWelcome to 'Guess My Number'!"
print "\nI'm thinking of a number between 1 and 100." 
print "Try to guess it in as few attempts as possible.\n"

# set the initial values
the_number = random.randrange(100) + 1
ask_number("Type a number: ", 1, 100)
tries = 1

# guessing loop

tries += 1
ask_number("Type a number: ", 1, 100)
def ask_number(question, low, high):
    """Ask for a name within a range."""
    response = None
    while response not in range(low, high):
        response = int(raw_input(question))
    return response    
            
    
    
ask_number("Type a number: ", 1, 100)
print "You guessed it!  The number was", the_number
print "And it only took you", tries, "tries!\n"
  
raw_input("\n\nPress the enter key to exit.")

Move definition to line 9, where it belongs. Whe two calls to ask_number?

Move definition to line 9, where it belongs. Whe two calls to ask_number?

would this code be the definition?

def ask_number(question, low, high):
    """Ask for a name within a range."""
    response = None
    while response not in range(low, high):
        response = int(raw_input(question))
    return response

Yes. Your function definition.

Yes. Your function definition.

so i take this

def ask_number(question, low, high):
    """Ask for a name within a range."""
    response = None
    while response not in range(low, high):
        response = int(raw_input(question))
    return response

and move it to line 9 but will that tell me when i am asking for a number if i need to go high or lower?

alright i moved my definition to line 9 it still won't let me guess until i get the number right or tell me if it too high or low?

thanks for yals help

No, you have not coded that yet.

All you need to do is check if their input is greater than or less than the number they're trying to guess.

Note: You've commented in your code where the guessing loop should go. Where is it? The code you have now will ask only three times because you call ask_number 3 times and then it will act as if their guess was correct. You don't need to do that, just put ONE call in a loop that continues until their response is equal to the number they're guessing.

No, you have not coded that yet.

All you need to do is check if their input is greater than or less than the number they're trying to guess.

Note: You've commented in your code where the guessing loop should go. Where is it? The code you have now will ask only three times because you call ask_number 3 times and then it will act as if their guess was correct. You don't need to do that, just put ONE call in a loop that continues until their response is equal to the number they're guessing.

so i need to have ask_number just once
would this be a good code to use for the guessing loop?

while (guess != the_number):
    if (guess > the_number):
        print "Lower..."
    else:
        print "Higher..."
            
    ask_number("type a number:" , 1,100)
    tries += 1

Except it is problem first time around. set guess to zero in beginning and do before line 2:

if guess:

Or change lines 4 to:

elif guess:
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.