Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 Function not returning expected value
#1
I have just started learning Python and I'm writing a simple text adventure game... I thought it would be a good idea to write a function that you could tell how many answers there are to pick from, then it would make sure the user enters an integer, and that it's within the range of answers.

So I wrote the code below, however if an incorrect number is entered, using the example below, let's say 6 was entered, it will correctly say that the number isn't valid and asks again, but then even if you put a correct number after that it will only return the first incorrect answer (6 in my example)

def user_input(number_of_answers):
    user_int = False
    while user_int == False:
        try:
            answer = int(input("> "))
            user_int = True
        except:
            print("That's not a number!")
    for i in range(1, number_of_answers + 1):
        print("i = ", i)
        print("answer = ", answer)
        if i == answer:
            print("Matched!")
            break
        elif i == number_of_answers:
            print("That answer is not valid")
            user_input(number_of_answers)
    return answer

user_answer = user_input(5)
print(user_answer)
I have since re-written this function and got it to work a different way - but I feel like there's probably an important lesson to learn here about functions, as I really can't see why it wouldn't work! So if anyone could tell me why it doesn't work I would be very grateful!
Quote
#2
So curious I thought all for i in range, had a starting value of (0, x) and why are you adding the +1? wouldn't that make a user entering a 1 = 2? and a 0 = 1? or a 5 = 6(which is invalid?
Quote
#3
Firstly, there's a potential recursion issue with the function. In the elif statement, the function is called again; I suppose that's to initiate the sequence again. If the user keeps entering invalid options, that will eventually lead to an overflow. Instead, try wrapping the entire function body in a while loop. You could indent the if statement to move it under the existing loop.

Also, you don't have to write "while user_int == false". You could do "while not user_int" instead.

It's returning the original value because your return statement is in the same local scope as the original answer. So, the function call in the elif statement of the original code is returning a valid answer. Then, the original function call is returning its answer which was invalid.

Here's how I rewrote it:

def user_input(number_of_answers):
    while True:
        try:
            answer = int(input("> "))
        except:
            print("That's not a number!")
            continue

        if 0 < answer < number_of_answers:
            return answer
        print("That answer is not valid!")
Quote
#4
Euqinu Wrote:but then even if you put a correct number after that it will only return the first incorrect answer (6 in my example)
That's because after the incorrect answer, you obtain a new value from the user by calling user_input() again, but you don't return the value. So replace line 17 above with
            return user_input(number_of_answers)
Quote
#5
Thank you all for your replies, I think I get it now - I thought that when you called a function from within itself that it would essentially be looping around... but I guess it's essentially starting a 'new' function, so the value of the variable 'answer' in the original function would stay unchanged.

stullis - that's quite similar to how I re-wrote it in the end, although you've managed to do it in a few less steps than me :)

I'm starting to really appreciate how complex coding can be... even the simplest little task can be written in hundreds of different ways! :/
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Function not returning correct value ActualNoob 3 484 Jan-11-2019, 12:35 AM
Last Post: stullis
  zip() function does not work as expected AyCaramba 1 2,219 Jan-31-2018, 05:25 PM
Last Post: Gribouillis
  Recursive function not returning expected output...(Python speech recog module) bigmit37 4 2,283 Jan-10-2017, 02:13 PM
Last Post: bigmit37
  function returning None tkj80 3 2,062 Oct-06-2016, 02:08 AM
Last Post: tkj80

Forum Jump:


Users browsing this thread: 1 Guest(s)