0

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.

bad_coder
  • 11,289
  • 20
  • 44
  • 72
crayons
  • 3
  • 3
  • That function will definitely print 0, 1, 2, 3, 4. However, you have to CALL the function. After you write the function, you need `main()` to force the function to be called. Other notes: It would be easier to use `for x in range(5):`. And we do not use outer parentheses in `if` and `while` statements. That's a leftover from C. – Tim Roberts Nov 14 '22 at 05:10
  • hi, you should calll your function – Ilya Nov 14 '22 at 05:22

1 Answers1

0

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()
kconsiglio
  • 401
  • 1
  • 8