功能:如何使它们应对多种选择?

时间:2019-06-27 12:18:10

标签: python-3.x

我最近创建了一个代码,该代码使我可以处理来自此新闻网站(tempo.co)的html并将其翻译成英语。我创建了一组函数来清理文本并使文本更易于使用,同时从网站的各个部分提取html。

Q1)由于该网站具有许多类别(经济,商业,政治,技术等),我想知道是否有一种方法可以简化我的功能,以便我不必手动为其中的每个类别进行编码新闻网站?请参见下面的代码。

Q2)对于我的函数“ site_category”,如果我输入错误的选项,如何使代码停止运行,即类似于内置的python函数“ break”?

def site_category (choice):

    if choice == "National":
        National = "https://nasional.tempo.co/"
        return National

    if choice == "Business":
        Business = "https://bisnis.tempo.co/"
        return Business

    else:
        print("Error, please try again.")

def doc_naming (choice, date):

    if choice == "National":
        filename = "Tempo-" + choice + " " + str(date) + ".docx"
        return filename

    if choice == "Business":
        filename = "Tempo-" + choice + " " + str(date) + ".docx"
        return filename

def remove_title_ends(choice, title):

    if choice == "National":
        new_title = title.strip("- Nasional Tempo.co")
        return new_title

    if choice == "Business":
        new_title = title.strip("- Bisnis Tempo.co")
        return new_title


def text_cleaner (text):
    truncate1 = text.find("text=") + 5
    truncate2 = text.find("pronunciation") - 2
    cleaned_text = text[truncate1:truncate2]

    return cleaned_text

choice = input("Enter 'National' for national news, 'Business' for business news: ")
category = site_category(choice)
print("You have chosen category: " + choice + "." )

date = input("Provide the date you are monitoring: ")
document_name = doc_naming(choice, date)
print("Your file name is " + document_name)

1 个答案:

答案 0 :(得分:0)

假设您要完全停止代码,则可以raise例外:

def site_category (choice):

    if choice == "National":
        National = "https://nasional.tempo.co/"
        return National

    if choice == "Business":
        Business = "https://bisnis.tempo.co/"
        return Business

    else:
        raise ValueError("Error, please try again.")

除非您在外部作用域中捕获到异常,否则这将退出并显示错误消息。有关更多信息,请参见the documention