-4

Ok guys, this is driving me crazy. I know java, but python is nuts. I'm just trying to writing a simple counter program using a for loop, but I can't do that because this language is made for weirdos. Haha, ok just kidding on that one, but seriously, what am I doing wrong?

class makeLines:
    def main():
       counter()
        
    def counter():
        for i in range(0,10):
            print(i)

When I run it, nothing happens. No output...

Rashid 'Lee' Ibrahim
  • 1,357
  • 1
  • 9
  • 21
Justin
  • 67
  • 6
  • 1
    Just `function_name()` But you don't need a class here, unlike java. You can just define both the `main` and `counter` function without the class, and call the `main()` or `counter()` depending on what you want – han solo Aug 25 '20 at 16:10
  • 2
    You never call anything. Also, the indentation here is broken. And you don't need a class, it's not Java. – Thierry Lathuille Aug 25 '20 at 16:11
  • 1
    `main` isn't a special function that's called automatically; unlike Java. – Carcigenicate Aug 25 '20 at 16:12
  • If you don't know anything about Python you should start with the tutorial (https://docs.python.org/3/tutorial/index.html) instead of making random guesses and being confused – UnholySheep Aug 25 '20 at 16:14

2 Answers2

0

You call functions in Python exactly as in Java:

function_name(args…)

Your code doesn’t work because its indentation is broken. Furthermore, there is no global code calling any function. Unlike Java, Python code doesn’t have to be inside classes, and there is no entry point. Just call the function directly:

def counter():
    for i in range(0,10):
        print(i)
        
counter()

You can create an entry point function, and this is in fact often a good idea. So you will see a lot of code in Python that looks as follows:

import sys


def main():
    ‹main logic here›


if __name__ == '__main__':
    sys.exit(main())

The reason for this is to make code usable as both a module and an executable.

Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
  • Ah thank you. One more question. If I want to create a list in a for loop in python and append to it, how would I do that? I'm thinking something along the lines of ....................for I in range(1,30) /n Erot[I]=i..............I'm thinking this wold automatically intitate Erot and append values to it. – Justin Aug 25 '20 at 16:28
  • @Justin Literally, `lst.append(i)` (and before the loop, `lst = []`). But for now you should step back and read a beginner’s book/tutorial for Python. You’re flying blind. Googling for “create a list in a for loop in python and append to it” also brings up tons of hits. – Konrad Rudolph Aug 25 '20 at 16:30
0

Erase your first and second lines, because you don't need any classes and main() function(it's not C++ or Java, there's no such thing like main function in Python) in that case. And you must add the space before your 'for' loop.

Viktor
  • 566
  • 5
  • 17