Home > database >  I Have A Question About While Loops and Writing One (In A Function) - Beginner
I Have A Question About While Loops and Writing One (In A Function) - Beginner

Time:11-15

I have run into a weird problem or maybe something I'm not understanding with this line of code that is kind of bugging me. I couldn't find what I needed online so thought I would ask here. When I was asked to put my while loop in a function I didn't get the result I was after and I'm very confused.

Here is the code I'm trying to mess around with:

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

The function is supposed to print from (o, 1, 2 ,3, 4,), Instead nothing prints and there is no error in the code, so it makes it even more confusing. Am I missing something? I'm using a newer version of piCharm if that helps.

CodePudding user response:

You have a function but do not call it so the code doesn't execute. You need to call your function main().

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

main()
  • Related