The Main() Function Appears to Not Work

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

C++ function in main doesn't work

You're accidentally declaring a function inside main() instead of calling it.

int main ()
{
void a(); // <-- DECLARES a function, does not call it
return 0;
}

Here is the fix:

int main ()
{
a();
return 0;
}

Also note that you probably want a newline:

void a()
{
std::cout<<"a\n";
}

Or you can use std::endl, if you like typing.

Doxygen not documenting main function in main.cpp

This is because you are documenting a global object which doxygen, by default, does not document. From the doxygen manual (emphasis mine):

To document a member of a C++ class, you must also document the class itself. The same holds for namespaces. To document a global C function, typedef, enum or preprocessor definition you must first document the file that contains it (usually this will be a header file, because that file contains the information that is exported to other source files).

Let's repeat that, because it is often overlooked: to document global objects (functions, typedefs, enum, macros, etc), you must document the file in which they are defined. In other words, there must at least be a

/*! \file */ 

or a

/** @file */ 

line in this file.

So try adding one of the above two lines to your main.cpp file.

Time.sleep() function doesn't seem to work properly while threading

I'm not sure what the others are talking about, this really isn't that much to handle. You even mentioned that the most essential part is right at the top.

The problem is actually quite simple: you create the thread inside main(), but you call main() recursively, so you're actually creating multiple threads, each with their own timers, none of which can stop on their own. The longer you play, the more hungerloss() threads are created, and thus the more timers are started. If you want, you can visualize this by saving a random number to the top of hungerloss() and printing it out every time the sleep timer finishes.

All you need to do is avoid recreating the same thread multiple times. The simplest way considering the structure of your current code would probably be to just move hungerloss() outside of main(), like so:

def hungerloss():
global hunger
while True:
time.sleep(10)
hunger -= 1.5

threading.Thread(target=hungerloss).start()

def main():
# you get the idea


Related Topics



Leave a reply



Submit