不会打印 int 和 str 的组合

Won't print a combination of int and str

购买散装药花时计算节省的愚蠢脚本。

尝试将其包装在 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

x.add_row(["$" + str(oneGP), "$" + str(twoGP), "$" + str(OneEightP), "$" + str(OneFourP),"$" + str(OneHalfP), "$" + str(ozP)])

您尝试将 int 连接到字符串,所以会发生此错误 + 将适用于相同类型的对象,因此您需要在某些情况下使用 cast 或者如果你不想使用 , 而不是 +