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

Prevent writing an infinite loop

This code takes user input and stores it into a file ,and then it saves the time the user visited in another separate file.

    import date time 
    Today = date time.date time.now()
    name = input('Whats your name :')
    filename = 'guests'

    with open (filename, 'a') as file_object:
      file_object.write(name +'.\n')
    while name:
       print('Welcome,how are you doing '  + name)
    filename = "guest_book"
    with open(filename,'a') as file_object:
        file_object.write(name + 'visited on ' + Today.string('%c') + '.\n') 
        run = False
    ```

 - List item

I always find myself writing infinite loops , this program works but i have to cancel out with control c. How can I avoid writing infinite loops.The variable `run = True` and 'run = False' I got from another post but I feel as if it isn't necessary.              

>Solution :

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

while name: will run for as long as name isn’t empty. You probably meant to write if name: which will run the code just once, if name isn’t empty.

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