I was given an assignment to create a high/low program (well, the first part anyway) that fulfills the following pseudocode:

• Print instructions to the user
• Start with the variables high = 1000, low = 1, and tries = 1
• While high is greater than low
◦ Guess the average of high and low
◦ Ask the user to respond to the guess
◦ Handle the four possible outcomes:
▪ If the guess was right, print a message that tries guesses were
required and quit the program
▪ If the guess was too high, print a message that says "I will guess
lower."
▪ If the guess was too low, print a message that says "I will guess
higher."
▪ If the user entered an incorrect value, print out the instructions
again

I am having problems with the while statement in the get_input definition, the way I have it in, I think it will require me to either return a variable or pass a variable to another definition (EG give_guess(response)) to avoid an unwanted loop. I'm unsure of how exactly to do either of those options. if that is not what I need to do then please help me to understand what it is that I need to do instead.

#guess the number
def give_instructions():
    print ("pick a number between 1 and 1000 and I will try to guess it")
    print ("in no more than 10 tries. After each guess, enter 0 if I got it")
    print ("right, -1 if I need to guess lower, and 1 if I need to guess")
    print ("higher")

high=1000
low=1
tries=1

def give_first_guess():
    total=high+low
    guess=total/2
    if high==low:
        print ("your number must be"), guess
        print ("that took"), tries, ("guesses")
    print ("my guess is:"), guess
    get_input()
    
def give_guess():
    total=high+low
    guess=total/2
    if response==0:
        print ("that took"), tries, ("guesses")
    if high==low:
        print ("your number must be"), guess
        print ("that took"), tries, ("guesses")
    print ("my guess is:"), guess
    get_input()
    
def get_input():
    response=input ("please enter -1, 0, or 1:")
    while high>low:
        if response==0:
            give_number()
        elif response==-1:
            print ("I will guess lower")
            give_number()
        elif response==1:
            print ("I will guess higher")
            give_number()
        else:
            print ("Enter 0 if I got it right, -1 if I need to guess lower, or 1")
            print ("if I need to guess higher")
            give_number()
    give_number()
    
def main():
    give_instructions()
    give_first_number()
    give_number()

main()

I realized that there are a few errors in my code as it is, I renamed a couple of the definitions (give_guess() and (give_first_guess()) and I forgot to change them further down in the code.
give_guess=give_number
give_first_guess=give_first_number
those don't have any affect on the problem I am having though, so I would be thankful for some help still.

Moved this to Forum Thread:
Please submit only finished/working code snippets. Questions like this should be submitted as a regular forum thread.

Edited 7 Years Ago by vegaseat: not a code snippet

have a look at the 'break' statement in the python docs.

thanks! that was exactly what I needed.

This question has already been answered. Start a new discussion instead.