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.
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
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.
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.
date time is actually ‘datetime
‘ and today. string is actually Today.strftime
but the post editor wouldn’t let me post it that way
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.