I'm trying to learn how to write a loop but it doesn't seem to print to the console

  • A+
Category:Languages

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 

 


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.

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: