Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

I Have A Question About While Loops and Writing One (In A Function) – Beginner

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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()
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading