0

I'm having some trouble printing my program in the compiler, I don't get any error messages but the program doesn't run. I can't see why really?

#1 USD = 0.7617 Euro
#1 Euro = 1.3128 USD

def currencyConvert():
    userChoice = raw_input ("Vad vill du konvertera? 1)USD -> Euro 2) Euro -> USD")
    if userChoice == "1":
        userUSD = input ("Hur mycket vill du konvertera ange i USD?")
        Euro = userUSD * 0.7617
        print "USD %0.2f", userUSD, "= %0.2f", Euro, "Euro"
    elif userChoice == "2":
        userEuro = input ("Hur mycket vill du konvertera ange i Euro?")
        USD = userEuro * 1.3128 
        print "%0.2f", userEuro, " Euro = USD %0.2f", USD
    else:
        print "error du angav fel val, försök igen ange val 1 eller 2"
        currencyConvert()
dansalmo
  • 11,506
  • 5
  • 58
  • 53
SterlinkArcher
  • 673
  • 3
  • 21
  • 36
  • 2
    You define the function but don't call it. Call your function as well. – aestrivex Jun 11 '13 at 17:11
  • 1
    Where do you actually call `currencyConvert()`? – Blender Jun 11 '13 at 17:11
  • if you are starting now use Python 3, there is no point in starting to learn python 2.x today. – user2384250 Jun 11 '13 at 17:13
  • 4
    @user2384250: That is not necessarily true. That depends *vastly* on what libraries you need; the Python 2 to 3 transition will take another 2 years or so to fully pan out. – Martijn Pieters Jun 11 '13 at 17:13
  • Also depends upon what you want to do with it. For instance, I use Python for CGI, and my hosting provider just flat-out doesn't have Python 3, yet, so Python 2 is all I have available there. – Crowman Jun 11 '13 at 19:07

4 Answers4

6

In the end of the file, add

if __name__ == '__main__':
    currencyConvert()

on it's own, without any indentation. This will actually call the procedure you have defined as soon as the script runs independently.

kqr
  • 14,791
  • 3
  • 41
  • 72
3

Add this to the end of your program:

if __name__ == '__main__':
    currencyConvert()
garnertb
  • 9,454
  • 36
  • 38
3

At the end of your script just add something like:

if __name__ == '__main__':
    currencyConvert()

It means that if it is the main section, it will be True. If you don't add this if, your function will be called when the script is imported as a module.

And then call your script using python script.py, for example.

Saullo G. P. Castro
  • 56,802
  • 26
  • 179
  • 234
1

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 :)

Community
  • 1
  • 1
2rs2ts
  • 10,662
  • 10
  • 51
  • 95