Again. Trying to teach myself how to use the return function. This is me trying to get a user to make a choice between 1-5 and then returning their choice. I don’t know now returns work but if someone can help me tackle this I’d greatly appreciate it.
print("PRACTICE.")
def get_choice():
choice = -1
while choice < 1 or choice > 5:
choice = int(input("Please make a choice (1-5): "))
return choice
As per your other thread, the function works fine for me, are you calling it (get_choice()
)?
(Jan-05-2019, 11:44 PM)ichabod801 Wrote: [ -> ]As per your other thread, the function works fine for me, are you calling it (get_choice()
)?
Hello. Yes I am calling it get_choice() after the Return choice line. However, it just stops running after the user inputs a number between 1-5. Is that supposed to happen?
Yeah, that's what it should do. It's designed to get a choice from 1-5. Once it gets that value, it returns it and stops.
Are you expecting it to do something else?
(Jan-07-2019, 05:34 PM)ichabod801 Wrote: [ -> ]Yeah, that's what it should do. It's designed to get a choice from 1-5. Once it gets that value, it returns it and stops. Are you expecting it to do something else?
Only it didn't return anything
Output:
PRACTICE.
Please make a choice(1-5): 2 (Entered by me)
>
How are you running that? If you run it from the system command line, you won't see the return value. If you run it in the Python interpreter you should see the output. This is what I get in the interpreter:
Output:
>>> get_choice()
Please make a choice (1-5): 3
3
>>>
Also note that you have to call it from within the interpreter to see it in the interpreter. In any case, if you change the call to
print(get_choice())
you'll see the return value.