Q: Python error in functions I am an absolute beginner in python. I was practicing simple python code of functions from a tutorial. But I am getting some wierd error when i try running this code snippet from terminal. What is the role of main in this can someone explain me?
def donuts(count):
  if count < 10:
    return 'Number of donuts: ' +str(count)
  else:
    return 'Number of donuts: many'

if __name__ == '__main__':
  main()


A: main is the conventional name for a function to run when a file is executed as a script. However, it's not magic; you still need to define what main is, or Python will throw a NameError at you. If you want to run donuts, have main run donuts:
def donuts(count):
  if count < 10:
    return 'Number of donuts: ' +str(count)
  else:
    return 'Number of donuts: many'

def main():
    print donuts(5)

if __name__ == '__main__':
  main()


A: IMO, the main function is usually created to aggregate all the defined functions into one. Implying that main() is used to call all other functions. Seems like you're calling main but main isn't defined. 
Rule of thumb, always define a function before you call it. 
