-3

here is my code

def help():
    f = input()
    print (f)

it works without def and doesn't work with it in place. I tried many different simple codes like this but received no output whenever I used def. I am using VS code.

tdelaney
  • 73,364
  • 6
  • 83
  • 116
  • You've to `call` the `help` function after `defining` it as: help() – Lekhnath Feb 17 '23 at 04:52
  • did you call the function at all after putting it in the function? call `help()` at the same indent level as `def help()`. – Shorn Feb 17 '23 at 04:53
  • `def`is just _defining_ the function. So you can call it later. If you don't call it, well... it works, but you're not asking it to do anything. – Ignatius Reilly Feb 17 '23 at 04:53
  • 1
    Does this answer your question? [Why doesn't the main() function run when I start a Python script? Where does the script start running (what is its entry point)?](https://stackoverflow.com/questions/17257631/why-doesnt-the-main-function-run-when-i-start-a-python-script-where-does-the) – Ignatius Reilly Feb 17 '23 at 04:55
  • Your code defines a function, but does nothing with that function, it is never called. so there will be no output, since the code in the function never executes – juanpa.arrivillaga Feb 17 '23 at 06:09
  • What is the question here? Please see [How do I ask a good question?](https://stackoverflow.com/help/how-to-ask) – TylerH Feb 17 '23 at 20:20

2 Answers2

4

Once a function is defined, you have to call it to make something happen.

def help():
    f = input()
    print (f)

help()

Note that it may still seem like its doing nothing because you don't prompt for data. You'll need to go to the terminal window and enter a string. Better, include a prompt with your input so you know what to do.

def help():
    f = input("Input data to print: ")
    print (f)

help()
tdelaney
  • 73,364
  • 6
  • 83
  • 116
  • 1
    +1, Further reading for the OP [What does if __name__ == "__main__": do?](https://stackoverflow.com/questions/419163/what-does-if-name-main-do) – flakes Feb 17 '23 at 04:57
0

You are defining the function but never calling it. You need to define it like

def help():
    f = input()
    print(f)
help()
Brandon Johnson
  • 172
  • 1
  • 6