如何要求用户再次如果他想把更多的链接在我的python代码



嗨,我有一个python代码,它按顺序执行以下操作

  1. 显示url列表
  2. 询问用户是否要删除任何链接。
  3. 如果用户输入yes,则取一个input which链接删除和删除链接
  4. 代码退出。

我的问题是我希望代码在删除链接后再次询问用户,如果他们想删除更多链接?

有谁能帮我弄明白吗?对不起,我是python的新手,所以如果这个问题看起来很微不足道。 我代码:

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

您可以将它放入while循环中,像这样,并替换sys.exit()来打破:

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

这是唯一的一种方法

最新更新