-2

I'm new to learning Python and using Eclipse + PyDev. I can't seem to figure out why my program isn't running.

Here is my code:

def main():
    print("Testing")
    test1 = float(input("Test1: "))
    test2 = float(input("Test2: "))
    test3 = float(input("Test3: "))
    calculate_cost (test1, test2, test3)

def calculate_cost (test1, test2, test3):
    print("Testing")

I am assuming I am missing something in one or both of the functions to make this run properly.

Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
user1804933
  • 453
  • 2
  • 7
  • 14

2 Answers2

6

This is Python, not C/C++, main() is not the entry point to your program, you have to invoke the function manually. Just call it:

# your code
# ...
main()

If you want to prevent that your code is called when importing from other place, then you want to use:

# your code
# ...
if __name__ == '__main__':
    main()

This question: What does if __name__ == “__main__”: do? explains this last block.

Community
  • 1
  • 1
jabaldonedo
  • 25,822
  • 8
  • 77
  • 77
3

Unlike some other languages, there is no enforcement of the convention that a main function is called when a program is executed. In Python, you will have to do that manually. That means you will have to put a main() at the bottom of your script to make your main function execute.

A common pattern is to use the following:

if __name__ == '__main__`:
    main()

This will execute the main function when the script is run directly, but prevents its execution when the script is imported as a module somewhere else. See this question for more details on that part.

Community
  • 1
  • 1
poke
  • 369,085
  • 72
  • 557
  • 602