user8854541
user8854541

Reputation:

Python program with main() method does not run

def main():
   names = []
   for line in ins:
       number_strings = line.split() # Split the line on runs of whitespace
       data.append(numbers_strings) # Add the "row" to your list.
       print(data)

I tried using this code to print a text file that looks like this

name num1 num2 C/N

I am trying to print this but when I run the command "python3 file.py" no output occurs. instead of printing the contents of the file I am putting in

Upvotes: 0

Views: 1180

Answers (2)

Tenoch G
Tenoch G

Reputation: 171

If you just want to execute that code you can forget about the main function and just write your code

names = []
   for line in ins:
       number_strings = line.split() # Split the line on runs of whitespace
       data.append(numbers_strings) # Add the "row" to your list.
       print(data)

If you do want to use a main function follow the community wiki answer.

Upvotes: -1

cs95
cs95

Reputation: 402872

Unlike in C, execution in python does not begin from the main method, as python follows a top-down approach. What you'll need to do is explicitly invoke the main method to have it run.

def main():
    ...

main()

If you want the main method to run only when invoked through the script (and not when imported), specify under what __name__ it should run:

def main():
    ...

if __name__ == '__main__':
    main()

For more information, read

Upvotes: 6

Related Questions