Reputation: 5
I have the following code:
print('WELCOME TO YOUR TASK MANAGER!')
#Asks the user for a filename.
filename = input('What would you like your filename to be: \n(Please type
\'.txt\' at the end of the name)');
#Creates an empty list and asks the user for the tasks needing to be written down and creates a file.
tasks = []
with open(filename, 'w+') as f:
prompt = 'Please enter what you need to do: \n When you are done putting in
tasks please type \'exit\' '
user_input = input(prompt).strip()
while (user_input != 'exit'):
tasks.append(user_input)
user_input = input(prompt).strip()
#Asks the user whether or not he/she would like to add, remove, or exit the program.
prompt1 = input('Would you like to add or remove a task? \nIf add please
type \'add\'. \nIf remove please type \'remove\'.\n If not please type
exit.')
if prompt1 == 'add':
prompt1 = input('Please say what you would like to add:')
tasks.append(prompt1)
if prompt1 == 'remove':
prompt1 = input('Please say what you would like to remove:')
tasks.remove(prompt1)
tasks.sort()
f.write(str(tasks))
else:
tasks.sort()
f.write(str(tasks))
#Outputs the list of the tasks to the user and tells the user where a copy of the list is saved.
print(tasks)
print('A copy of your tasks is in the file \'{}\''.format(filename))
I would like to be able to run the prompt1 input as many times as needed until the user enters exit. However, when running the code it only allows the user to enter one of the 3 choices: add, remove, or exit. Any ideas on how I could write the code to allow multiple entries from prompt 1 after the first entry is submitted?
Upvotes: 0
Views: 58
Reputation: 21
I hopes this is the code, what are you looking for.
#!/usr/bin/python3
import sys
print('WELCOME TO YOUR TASK MANAGER!')
#Asks the user for a filename.
filename = input("What would you like your filename to be: \n(Please type\.txt\' at the end of the name)");
#Creates an empty list and asks the user for the tasks needing to be written down and creates a file.
tasks = []
while True:
prompt = 'Please enter what you need to do: \n When you are done putting in tasks please type \"exit\"" '
user_input = input(prompt).strip()
if user_input == 'exit':
with open(filename, 'w+') as f:
f.write(tasks)
sys.exit()
else:
tasks.append(user_input)
#Asks the user whether or not he/she would like to add, remove, or exit the program.
prompt1 = input('Would you like to add or remove a task? \nIf add please type \'add\'. \nIf remove please type \"remove\".\n If not please type exit.')
if prompt1 == 'add':
prompt1 = input('Please say what you would like to add:')
tasks.append(prompt1)
elif prompt1 == 'remove':
prompt1 = input('Please say what you would like to remove:')
tasks.remove(prompt1)
tasks.sort()
f.write(str(tasks))
else:
if user_input == 'exit':
with open(filename, 'w+') as f:
f.write(tasks)
sys.exit()
Upvotes: 1
Reputation: 3770
You can try something like this :
a = input("Enter a file name: ")
task = []
while a != ("stop"):
#if else logic
.....
print(task)
a = input("Enter a word: ")
Upvotes: 1
Reputation: 2110
This should work:
tasks = []
with open(filename, 'w+') as f:
prompt = 'Please enter what you need to do: \n When you are done putting in
tasks please type \'exit\' '
prompt1 = input(prompt).strip()
while (prompt1 != 'exit'):
tasks.append(user_input)
#Asks the user whether or not he/she would like to add, remove, or exit the program.
prompt1 = input('Would you like to add or remove a task? \nIf add please
type \'add\'. \nIf remove please type \'remove\'.\n If not please type
exit.')
prompt1 = prompt1.strip()
if prompt1 == 'add':
prompt1 = input('Please say what you would like to add:')
tasks.append(prompt1)
elif prompt1 == 'remove':
prompt1 = input('Please say what you would like to remove:')
tasks.remove(prompt1)
tasks.sort()
f.write(str(tasks))
else:
tasks.sort()
f.write(str(tasks))
#Outputs the list of the tasks to the user and tells the user where a copy of the list is saved.
print(tasks)
print('A copy of your tasks is in the file \'{}\''.format(filename))
Changes made:
Upvotes: 3