简体   繁体   中英

Running if-else statements more than once

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?

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:

  • Used elif instead of second if. Else even if first condition satisfies, second condition need also be treated.
  • replaced the user_input variable with prompt1 itself
  • Brought printing the tasks outside the user input loop

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: ")

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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM