-1

So I am trying to get my Ask_Number to cooperate with my guessing game. For some reason the game code wont reconize that the Response to the Ask_Number is an interger. I've tried to define response as well and that just ends up breaking the code in a different way. I am going insane with this stuff. Here is the code:

import sys

def ask_number(question, low, high):
    """Ask for a number from 1 to 100."""

    response = int(input(question))

    while response not in range(low, high):
        ask_number("Try again. ", 1, 100)
        break
    else:
        print(response)
        return response


ask_number("Give me a number from 1-100: ", 1, 100)

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. OR ELSE!!!\n")
le_number = ask_number
guess = int(input("Take a Friggin Guess: "))
tries = 1
# guessing loop
while guess != le_number:
    if guess > le_number:
        print("Lower Idgit....")
    else:
        print("Higher Idgit...")

    if tries > 5:
        print("Too bad Idgit. You tried to many times and failed... What a shocker.")
print("The number was", le_number)
input("\n\nPress the enter key to exit Idgit")
sys.exit()

guess = int(input("Take a guess:"))
tries += 1


print("You guessed it! The number was", le_number)
print("And it only took you", tries, "tries!\n")
input("\n\nPress the enter key to exit Idgit")

If you guys could help me shed some light that would be great.

Patrick Artner
  • 50,409
  • 9
  • 43
  • 69

1 Answers1

1

This is confusing:

response = int(input(question))

while response not in range(low, high):
    ask_number("Try again. ", 1, 100)
    break                              # will leave the loop after 1 retry
else:                                  # so the while is superflous 
    print(response)                    # and the else part is never executed if you break
    return response                    # from a while, only if it evaluates to false

and this:

le_number = ask_number

does not execute the function - you need to call it:

le_number = ask_number("some question", 5,200) # with correct params

# or
while guess != ask_number("some question", 5,200):    # with correct params

It would be better to do "more" inside the function. The function of this function is to provide you with a number - all that is needed to do so is inside it - you can easily test/use it and be sure to get a number from it (unless the user kills your program, dies or the computer crashes):

def ask_number(question, low, high):
    """Ask for a number from low to high (inclusive)."""
    msg = f"Value must be in range [{low}-{high}]"
    while True:
        try:
            num = int(input(question))
            if low <= num <= high:
                return num
            print(msg)
        except ValueError:
            print(msg)


ask_number("Give me a number: ",20,100)

Output:

Give me a number: 1
Value must be in range [20-100]
Give me a number: 5
Value must be in range [20-100]
Give me a number: tata
Value must be in range [20-100]
Give me a number: 120
Value must be in range [20-100]
Give me a number: 50

This way only valid values ever escape the function.

For more infos please read the answers on Asking the user for input until they give a valid response


the_given_number = ask_number("Give me a number: ",20,100)

# do something with it 
print(200*the_given_number)  # prints 10000 for input of 50
Patrick Artner
  • 50,409
  • 9
  • 43
  • 69
  • thank you for the response. Though what I'm trying to make happen is the guessing game below recognizes the Ask_Number as an integer that's all. It wont for some reason – William Kleinknecht Barasu13 Feb 17 '19 at 21:40
  • @will You need to store the number in a variable and then can use it - it is an integer ...see edit – Patrick Artner Feb 17 '19 at 21:43
  • Thank you so much i will keep both of these responses in mind for next tme i deal with this. Thank you again – William Kleinknecht Barasu13 Feb 17 '19 at 21:53
  • @wil edited - there are several other things that confuse - best thing to do for posting here is create a [mcve] that replicates your problem without needless lines - like f.e. the line `ask_number("Give me a number from 1-100: ", 1, 100)` or `guess = int(input("Take a Friggin Guess: "))` - the first does nothing with the number it might get, the latter should simply use `ask_number(question, low, high)` as well ... – Patrick Artner Feb 17 '19 at 21:55