哑脚本,用于计算购买批量医疗花时的节省。
尝试将其包装在str()中,以查看是否将其全部转换为字符串,以便可以进行print()。
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)
回溯(最近一次通话最后一次):文件“ main.py”,第10行,在 x.add_row([“ $” + oneGP,“ $” + twoGP,“ $” + OneEightP,“ $” + OneFourP,“ $” + OneHalfP,“ $” + ozP])TypeError:只能连接 str(不是“ int”)到str
答案 0 :(得分:1)
x.add_row(["$" + str(oneGP), "$" + str(twoGP), "$" + str(OneEightP), "$" + str(OneFourP),"$" + str(OneHalfP), "$" + str(ozP)])
您尝试将incat转换为字符串,所以会发生此错误+可以与相同类型的对象一起使用,因此在某些情况下需要使用强制转换
或者如果您不想投射,请使用,
代替+