如何在def函数中打印

时间:2018-07-02 17:53:29

标签: python python-3.x

我有这个程序:

Option Explicit
Public Sub GetInfo()
    Dim sResponse As String, HTML As New HTMLDocument
    Const BASE_URL As String = "https://www.icd10data.com/Convert/"
    Dim ICDs(), currICD As Long
    ICDs = Array("S92.311D")

    With CreateObject("MSXML2.XMLHTTP")
        For currICD = LBound(ICDs) To UBound(ICDs)
            .Open "GET", BASE_URL & ICDs(currICD), False
            .send
            sResponse = StrConv(.responseBody, vbUnicode)
            sResponse = Mid$(sResponse, InStr(1, sResponse, "<!DOCTYPE "))

            With HTML
                .body.innerHTML = sResponse
                Debug.Print .getElementsByClassName("pageHeading")(0).innerText
                Debug.Print .getElementsByClassName("contentBlurbConversion")(0).innerText
            End With
        Next currICD
    End With
End Sub

您写了一个名字(本例中为视频游戏名称)会做什么,它会检查它是否在列表中,如果不是,则添加它,以便程序将其删除。 问题是输出没有函数中的消息,我也不知道为什么。

2 个答案:

答案 0 :(得分:0)

由于您要从list_o_matic返回消息,因此应该只打印呼叫者的返回值:

games = ['tlou', 'hitman', 'rainbow 6', 'nba2k']
print(games)
def list_o_matic(inp):
    if inp == "":
        games.pop()
        return "the game " + inp + " was deleted"
    elif inp in games:
        games.remove(inp)
        return "the game " + inp + " was removed"
    elif inp != games:
        games.append(inp)
        return "the game " + inp + " was added"
while True:
    if not games:
        print("goodbye!")
        break
    else:
        inp = input("write the name of the game: ")
        if inp == 'quit':
            print("goodbye!")
            break
        else:
            print(list_o_matic(inp))
            print(games)

或者,如果您希望按照标题提示在函数内打印消息,则在不返回消息的情况下打印消息:

games = ['tlou', 'hitman', 'rainbow 6', 'nba2k']
print(games)
def list_o_matic(inp):
    if inp == "":
        games.pop()
        print("the game " + inp + " was deleted")
    elif inp in games:
        games.remove(inp)
        print("the game " + inp + " was removed")
    elif inp != games:
        games.append(inp)
        print("the game " + inp + " was added")
while True:
    if not games:
        print("goodbye!")
        break
    else:
        inp = input("write the name of the game: ")
        if inp == 'quit':
            print("goodbye!")
            break
        else:
            list_o_matic(inp)
            print(games)

答案 1 :(得分:0)

您可以在代码print(list_o_matic(inp))中进行修改,因为您的函数已经返回了字符串。