不会打印int和s的组合

2024-05-19 00:42:03 发布

您现在位置:Python中文网/ 问答频道 /正文

愚蠢的脚本计算节省时,购买大量的医疗花

尝试将其包装在str()中,以查看是否将其全部转换为字符串,以便打印()


from prettytable import PrettyTable
    print("*Enter with spaces seperating")
    oneGP, twoGP, OneEightP, OneFourP, OneHalfP, ozP= map(int, input().split(" "))

    x = PrettyTable()

    x.field_names = ["1g", "2g", "1/8", "1/4", "1/2" "Oz"]

    x.add_row(["$" + oneGP, "$" + twoGP, "$" + OneEightP, "$" + OneFourP,"$" + OneHalfP, "$" + ozP])
    x.add_row([oneGP/1, twoGP/2, OneEightP/3.5, OneFourP/7,OneHalfP/14,ozP/28])

    print(x)

Traceback (most recent call last): File "main.py", line 10, in x.add_row(["$" + oneGP, "$" + twoGP, "$" + OneEightP, "$" + OneFourP,"$" + OneHalfP, "$" + ozP]) TypeError: can only concatenate str (not "int") to str


Tags: 脚本addintrowprint医疗str节省
1条回答
网友
1楼 · 发布于 2024-05-19 00:42:03
x.add_row(["$" + str(oneGP), "$" + str(twoGP), "$" + str(OneEightP), "$" + str(OneFourP),"$" + str(OneHalfP), "$" + str(ozP)])

您试图将int转换为string,因此发生此错误+将使用相同类型的对象,因此在某些情况下需要使用cast 或者如果您不想强制转换,请使用,而不是+

相关问题 更多 >

    热门问题