2
def you ():
    gi=input("what up")
    if gi in ["Nothing much", "nothing much", "nothing"]:
        print("cool")

you()
elif gi in ["stuff"]:
    print("sounds good")

I apparently cannot do this, however In a bigger program I am working on, I have a function that is used twice, however based of the user input, different results happen, so I tried to include my large function, and like the above program have an if else be continued throughout the function, because there are different options for user input, based on where the person is in the program.

I am just trying to figure out a good way to run a game, where the beginning is the same (function), however the game can progress in different ways, therefore a function won't work for all of it, so I just want to know if I can have an if else statement run through a function like in the small example above?

halfer
  • 19,824
  • 17
  • 99
  • 186
  • All if statements are localized to the block in which they occur. So no. – James Nov 08 '17 at 01:16
  • so when I referred to a larger program, I can't do this, and since the functions each have incomplete if else statements, functions are probably not the best way to go about making a large program? – QuestionedE Nov 08 '17 at 01:18
  • Functions are very helpful (well, actually necessary) for large programs. You need to learn how they can be used in the right way. Programming is much about subdividing problems in smaller parts, then in even smaller parts and finally grouping the smallest parts together again (mainly with functions, sometimes also objects and other stuff). – Michael Butscher Nov 08 '17 at 01:28

1 Answers1

0

As James already said, no you cannot continue an if statement outside of a function. Referring to your question if functions are useful in a larger program: yes they are. You can use data generated in functions in your main program or other functions by returning your results. For instance:

def you ():
   gi=input("what up")
   return gi

result = you()
if result in ["Nothing much", "nothing much", "nothing"]:
   print("cool")
elif result in ["stuff"]:
   print("sounds good")
pTz
  • 144
  • 3
  • Thanks, that helps a ton! Just for clarification, when you put "result = you()", what exactly does that do, does that set the value of gi to "you()", or "result"? Or am I just really overthinking this? – QuestionedE Nov 08 '17 at 01:29
  • result = you() assigns the value of gi to the variable result so you can use it in your main program. you() has no value. It is just a function. – pTz Nov 08 '17 at 01:32
  • Thank you! This has been so helpful! I will definitely use this, everyone who has responded to this has helped me out so much, thanks guys! – QuestionedE Nov 08 '17 at 01:34
  • Hi, sorry to bother you again, but I perfectly implemented the above situation in my code, but whatever the "result" ended up being, the you() function just went ahead and ran, then if I entered the "result" correctly, the right program ran, I am really confused, any way you can help? – QuestionedE Nov 08 '17 at 02:23
  • I am not sure if I understand the problem but the code is a sequence of operations. What happens in `result = you()` is the following: First, the complete body of the function you is executed, i.e. the input and the return of the input to the variable result in your main program. After that the result is inspected in the if statement and depending on what the result is you have these two outputs. – pTz Nov 08 '17 at 02:30
  • Is there any way I can show you my large program, I am quite flummoxed on why my program is not running? – QuestionedE Nov 08 '17 at 02:31