Basic Python Function Definition Question

Question:

I am a newly learning python I had a question for the community regarding Python function definitions or personal made function definition I am doing an exercise where you make a function that takes an input in celsius and spits out the conversion in Fahrenheit I’ve made a successful function but for some reason this one does not run correctly when I save it as a script and run it in the python terminal I get nothing. appreciate any help.

def TempChange():

    print("Input Cel Value")

    Cel = input()

    Cel = float(Cel)

    print ("transfering to fair value")

    Fair = (Cel * 9 / 5 + 32)

    print(Fair)

    while True:
        break

if I run the code without the def function at the top it works fine and chapgpt3 tells me there’s errors but spits out the same code

I tried without the def function on top and it worked as I expected it to.

Asked By: Alek6723

||

Answers:

There is a fundamental difference between defining a function and calling it.

The code inside a function definition is only executed when the code is called.

If I create a file "hello.py" as below, and execute it, nothing happens because I haven’t called any function(s).

def hello():
  print("hello")

Now, if I change that file to include a function call:

def hello():
  print("hello")

hello()

Now executing this program will print hello to standard output.

Answered By: Chris
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.