0

Hi I have a python code which does the following things sequentially

  1. Displays a list of urls
  2. asks the user if they want to remove any links.
  3. If the user inputs yes then takes an input which link to remove and removes the link
  4. code exits.

My issue is I want the code to ask the user again after removing the link if they want to remove more links?

Can someone help me figure it out. Sorry I am new to python so if this question seems very trivial.

My code:

import sys

def remove_links():
        # initializing list
        list = links
    
        # initializing string 
        str_to_remove = input("Enter the link you want to remove: ")
    
    
        # Remove List elements containing String character
        # Using list comprehension
        links.remove(str_to_remove)
    
        # printing result 
        print("The list after removal : " + str(links)) 
        print(len(links))


# printing original list
print("The list of links are : " + str(links))
print(len(links))

# Sets to simplify if/else in determining correct answers.
yesChoice = ['yes', 'y']
noChoice = ['no', 'n']

# Convert their input to lowercase.
choice = input("Do you want to remove some/any links? (y/N) ").lower()
if choice in yesChoice:
    remove_links()
elif choice in noChoice:
    # exit the code
    sys.exit("User doesn't want to make any modifications.")
else: 
    # print("Invalid input.\nExiting.")
    sys.exit("Invalid input.\nExiting.")
mk23
  • 1,257
  • 1
  • 12
  • 25

1 Answers1

1

You can put it in while loop, like this and reaplace sys.exit() to break:

while True:
    choice = input("Do you want to remove some/any links? (y/N)").lower()

    if choice in yesChoice:
        remove_links()
    elif choice in noChoice:
        print("User doesn't want to make any modifications.")
        break
    else:
        print("Invalid input.\nExiting.")
        break

This is only one way to do this

tonysdev
  • 126
  • 5