Handling loops in functions

This is the place for queries that don't fit in any of the other categories.

Handling loops in functions

Postby venator87 » Mon Aug 19, 2013 11:13 pm

I'm new to using functions. I'm currently trying to work a function for a simple game program where one guesses a number between 1-100. I'm trying to figure out how to work the loops correctly within the function.

1.) For the while response not in range(low, high) loop, even when the number entered is correctly within range the loop still goes through for some reason.

2.) After that when you re-enter your number again, the "lower.../higher..." condition statements will go through, but the loop will end after that.

I'd like to know how I'm putting together loops in the function, especially where I place return response.

Code: Select all
# 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.")

# set the initial values
low = 1
high = 100
the_number = random.randint(1, 100)

def ask_number(guess, low, high):
    """Ask for a number within a range."""
    response = None
    # Loop whether the number is not within range
    while response not in range(low, high):
        response = int(input("\nNot Valid. Please take another guess: "))

    # Loop whether the number is       
    while guess != the_number:
        if guess > the_number:
            print("\nLower...")
            return response
        elif guess == the_number:
            print("You guessed it!  The number was", the_number)
            return response
        else:
            print("\nHigher...")
            return response
   
   
def main():
    ask_number(int(input("\nTake a guess: ")),low, high)

main()
input("\n\nPress the enter key to exit.")
venator87
 
Posts: 5
Joined: Sat Mar 09, 2013 9:43 pm

Re: Handling loops in functions

Postby stranac » Tue Aug 20, 2013 1:47 pm

venator87 wrote:1.) For the while response not in range(low, high) loop, even when the number entered is correctly within range the loop still goes through for some reason.

The first number entered is named guess, not response.

venator87 wrote:2.) After that when you re-enter your number again, the "lower.../higher..." condition statements will go through, but the loop will end after that.

When you return from the function, the function(and the loop within it) is finished.
Those return statements are completely pointless.
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1093
Joined: Thu Feb 07, 2013 3:42 pm

Re: Handling loops in functions

Postby venator87 » Tue Aug 20, 2013 9:37 pm

Ok, got it! Thank you very much.
venator87
 
Posts: 5
Joined: Sat Mar 09, 2013 9:43 pm


Return to General Coding Help

Who is online

Users browsing this forum: Guitarman2010 and 2 guests