Prevent writing an infinite loop

Question:

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.              
Asked By: Roaming Status

||

Answers:

date time is actually ‘datetime‘ and today. string is actually Today.strftime but the post editor wouldn’t let me post it that way

Answered By: Roaming Status

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.

Answered By: cadolphs
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.