Main function


# function to convert integer to ordinal number
#@param number

def main(num):
    if num == 1:
        print ("one")
    elif num == 2:
        print ("two")
    elif num == 3:
        print ("three")
    elif num == 4:
        print ("four")
    elif num == 5:
        print ("five")
    elif num == 6:
        print ("six")
    elif num == 7:
        print ("seven")
    elif num == 8:
        print ("eight")
    elif num == 9:
        print ("nine")
    elif num == 10:
        print ("ten")
    elif num == 11:
        print ("two")
    else:
        print ("twelve")

if__name__ == "__main__":
    main()


why am l having a syntax error in the last line

Check your spacing carefully. I believe you’ll also be bitten here by your argument list.

If __name__ == "__main__" is a method of running parts of this module on its own. If the script itself was called script.py we could call python script.py (providing it with a __name__ of "__main__") from the command line to run this script but we could also import this module to make use of the function without main() running every single time. Have a web search if you want more info about why this is used.

1 Like

from what l read the last code is written to ensure that the program doesn’t run if imported into another program,
I also used the suggested code by the IDE but it still showed syntax error, I have tried checking spaces too

Assuming this is isn’t a mistake when posting rather than in the actual code-

if__name__ == "__main__"
  ^  # spacing?

That is an error, now it is telling me num is not defined

num is a parameter for main. When you’re calling main on line 33, you need to pass in a valid argument. num is not a valid argument because it has not been defined as a variable. You need to define the num variable, then give it a value, and pass that in on line 33. I’d also suggest you use a variable name other than num for this purpose so that the argument and parameter are not confused.


Currently, your function also prints two when num == 11.

1 Like

ok thank you, I also just found out the difference between a parameter and argument

1 Like