Main() Function Doesn't Run When Running Script

main() function doesn't run when running script

You still have to call the function.

def main():  # declaring a function just declares it - the code doesn't run
print("boo")

main() # here we call the function

Python main function not working

You need to call main(). Right now it is just a definition. What use is an entry in a dictionary if nobody uses the word?

def print1():
print("this is also a function")
def print2():
print("this is a function")

def main():
print1()
print2()

main()

It is common in Python programs to do things differently depending on if the file is being imported or run. When a file is executed, the __name__ variable is set either to '__main__' or the name of the file. It is set to '__main__' if the file is being executed as a python script, and it is set to the name of the file if it is being imported. You can use this information so that you don't actually run anything if it is just being imported instead of being run as a python script:

if __name__ == '__main__':
main()

That way, you can import the module, and use the functions without main() being called. If it is run as a python script, however, main() will be called.

Why is my Python function not being executed?

It sounds like you need to do this:

def main():
wiki_scrape()
all_csv()
wiki_set = scraped_set('locations.csv')
country_set = all_set('all.csv')
print wiki_set

main() # This calls your main function

Even better:

def main():
wiki_scrape()
all_csv()
wiki_set = scraped_set('locations.csv')
country_set = all_set('all.csv')
print wiki_set

if __name__ == '__main__':
main() # This calls your main function

Then run it from the command line like this:

python file_name.py

The built-in variable __name__ is the current contextual namespace. If you run a script from the command line, it will be equivalent to '__main__'. If you run/import the .py file as a module from somewhere else, including from inside the interpreter, the namespace (inside of the context of the module) will be the .py file name, or the package name if it is part of a package. For example:

## File my_file.py ##
print('__name__ is {0}'.format(__name__))
if __name__ = '__main__':
print("Hello, World!")

If you do this from command line:

python my_file.py

You will get:

__name__ is __main__
Hello, World!

If you import it from the interpreter, however, you can see that __name__ is not __main__:

>>> from my_file import *
>>> __name__ is my_file

Python code doesn't execute. No errors showed

You defined the main function, but you never call it.

add main() to the end of your code.

It then runs but there are errors, to fix the errors, change your main() function to

def main()
euro = int(input("Type in your EURO ammount: \n"))
cent = int(input("Type in your CENT ammount: \n"))
x = Money(euro, cent)
print (x)

Full working code:

class Money (object):
def __init__ (self, euro, cent):
self.euro = euro
self.cent = cent

def __str__ (self):
if self.cent >= 100:
r = self.cent / 100
self.cent = self.cent % 100
self.euro = self.euro + r
return ("%d EUR & %d cents") % (self.euro, self.cent)
else:
return ("%d EUR & %d cents") % (self.euro, self.cent)

def changeCent (self):
#100 c = 1 E
cents = self.euro * 100
self.cent = self.cent + cents
return self.cent

def changeSum (self, euros):
#1 E = 100 c
euros = self.cent / 100
self.euro = self.euro + euros
return self.euro

def debt (self, years, rate):
value = Money()
multiply = rate * years * 12 / 100
value.euro = self.euro * multiply
value.cent = self.cent * multiply
if value.cent > 100:
euro_ = value.cent / 100
value.cent = value.cent - 100
value.euro = value.euro + euro_
return value

def main():
euro = int(input("Type in your EURO ammount: \n"))
cent = int(input("Type in your CENT ammount: \n"))
x = Money(euro, cent)
print (x)

main()

Functions not executing in Python

Problem 1 is that you define a function ("def" is an abbreviation of "define"), but you don't call it.

def new_directory(): # define the function
if not os.path.exists(current_sandbox):
os.mkdir(current_sandbox)

new_directory() # call the function

Problem 2 (which hasn't hit you yet) is that you are using a global (current_sandbox) when you should use an argument -- in the latter case your function will be generally useful and even usefully callable from another module. Problem 3 is irregular indentation -- using an indent of 1 will cause anybody who has to read your code (including yourself) to go nuts. Stick to 4 and use spaces, not tabs.

def new_directory(dir_path):
if not os.path.exists(dir_path):
os.mkdir(dir_path)

new_directory(current_sandbox)
# much later
new_directory(some_other_path)

Kotlin script: main function does not get called locally, unlike in online judge

I haven't found a way to do this with Kotlin script files, but you can also use normal .kt files without having any classes in the file (my understanding is that Kotlin magically turns them into Java class bytecode/files):

kotlinc a.kt && kotlin AKt < a.in

This "runs" a.kt with standard input from a.in.

(And yes I only found this after I already wrote the question)



Related Topics



Leave a reply



Submit