简体   繁体   中英

Basic Python Function Definition 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.

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.

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