1

I'm practicing Python and i'm trying to write a loop but it won't print when i run it. I'm using Python 2.7 through jupyter notebook. When I run the code all it does is bring up another kernel but doesn't print anything.

def main():
    x = 0

    while (x < 5):
        print (x)
        x = x + 1
cs95
  • 379,657
  • 97
  • 704
  • 746
Evan
  • 13
  • 4

4 Answers4

6

You've defined the function, but now you have to tell Python to run it!

All you need to do is to call it, like so:

def main():
    x = 0

    while (x < 5):
        print (x)
        x = x + 1
main() #This is calling a function

Additionaly, you might want to change your x = x + 1 line to x += 1. They're equivalent, but it's much neater like this, and it saves you having to type the variable twice.

Within the jupyter notebook (or in the interactive mode of cmd), you can also do this after pressing shift + enter and calling the main() again.

Bancho
  • 116
  • 4
  • Thank you, this allowed it to run. However, the video i'm watching didn't have to call the function. They left the code like i originally had it. Any thoughts as to why that is? – Evan May 27 '18 at 23:28
  • Link the video, maybe they just skipped it – Bancho May 27 '18 at 23:38
  • it's through Lynda which is a free class through our local library. i figured out that they had defined at the bottom of the code that i couldn't see. thanks for all your help. – Evan May 28 '18 at 00:03
2

I'm guessing you've worked with C or one of its relatives, where the entry point of a program is a call to main. That's not how it is in Python. Python works like many scripting languages, running the file from top to bottom, and your file contains one task for it to do: define a function named main. The tradition in scripts with such a function is to put a test at the bottom to call it, allowing a choice between importing the code and running it as a program:

if __name__ == '__main__':
    main()

With this little epilogue, your program should actually run the main function.

There are a couple of other C-isms in your program as well. Python doesn't need parenthesis in while or if tests, and we have a more convenient for that operates using iterators instead of integers. For when integers are needed, range is convenient:

for x in range(5):
    print(x)

If you're running Python 2, print is a statement that doesn't need parenthesis, but it is a function in Python 3 so I kept them.

Yann Vernier
  • 15,414
  • 2
  • 28
  • 26
1

Remove your main function, by deleting def main(): and just specify x=0 before the while loop

AGN Gazer
  • 8,025
  • 2
  • 27
  • 45
Jacob
  • 45
  • 8
1

In your code, you define a function main() but you never call it. To fix this, either remove def main() from the cell, that is, simply execute the code that you want to run

x = 0
while (x < 5):
    print (x)
    x = x + 1

or call your function main():

def main():
    x = 0
    while (x < 5):
        print (x)
        x = x + 1

main()
AGN Gazer
  • 8,025
  • 2
  • 27
  • 45