Three answers showing you to use if __name__ == '__main__':
, but I don't see them explaining why you do this.
When you run a .py
file with python
, it executes each statement in the file, line by line. Of course, python statements with no indentation have the highest scope. You may think of them as the "most important" lines. They will always be executed, unlike lines underneath a conditional statement which does not evaluate to True
.
You don't see your function being called because the statement you have is a def
, which means that every indented line of code following the def
and until a return
or a line of code which is at the same level of indentation as the def
will be treated as a definition of a function, and will not be executed until that function is called. (In other words, the function simply isn't executed by the def
.)
If you wrote this, you would see the function execute:
def currencyConvert():
# your code here
currencyConvert()
However, the reason why you see the other users telling you to use if __name__ == '__main__':
is the behavior of a python import
statement. import
tells the program to execute every line of code in the imported module - which means that if you have the above code, you will execute currencyConvert()
if you import your file.
The way to fix this is to use the if __name__ == '__main__':
condition, in which you will put all the code you wish to run if your script is the program you are running, i.e. the .py
file called with python
or the file you run in your IDE. This tells the program to only execute the code underneath the if
when it is being run as the "main" module.
See this question for more. In the meantime, do what the other three guys told you to do to get your program working :)