简体   繁体   中英

Access a previously returned value - Python3.2

I have been trying for a while to be able to access my recently returned value and use it in if statements without having to recall the value.

Basically I have a while loop that calls a function that allows the user to input and then returns the input back into the loop.

while selection() != 0: ## Calls the "WHAT WOULD YOU LIKE TO DO" list and if it is 0 quits the script
    input() ## just so it doesn't go straight away
    if selection.return == 1: ## This is what I would like to happen but not sure how to do it... I've googled around a bit and checked python docs

See if I put:

if selection() == 1:

it will work but displays the "WHAT WOULD YOU LIKE TO DO" text again...

I'm sorry if this is an obvious solution, but the help would be very much appreciated :)

That's why you would store the result in a variable, so you can reference it in the future. Something like:

sel = selection()
while sel != 0:
    input()
    if sel==1:
        ...
    sel = selection()

This is just an alternative to the posted answer (it is too awkward to put in a comment), but please don't change your answer :) Whether or not you like it better is somewhat a choice of preference, but I like not having to repeat the input source line although it does "fog up" the loop condition:

while True:
    sel = selection()
    if sel == 0: # or perhaps "if not sel"
        break
    input()
    if sel == 1:
        ...

Happy coding.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM