使用python

时间:2017-10-20 22:56:41

标签: python

我对python很新,我正在接受一堆输入,好像它是迷你假期的收据,并尝试将所有三个类别添加到总计。我不断得到无法连接'str'和'int'对象的问题,无论我做什么,我都无法解决这个问题。

def main():
    airfareCost = int(input ("Airfare Cost"))
    hotelCost = int(input ("Hotel Cost"))
    mealsCost = int(input ("Meals Cost"))
    Total = (int(airfareCost + hotelCost + mealsCost))
    print "Mini-vacation time!"
    print "Below lists the expenses for your trip to the Florida Keys."
    print " "
    print "  Item                Cost"
    print "  Airfare:            "+"$"+int(airfareCost)
    print "  Hotel:              "+"$"+hotelCost
    print "  Meals:              "+"$"+mealsCost
    print "  ---------------------------"
    print "  Total:              "+"$"+Total
    print "  Have a fantastic trip!"

main()

2 个答案:

答案 0 :(得分:1)

您需要将整数值显式地转换为字符串。

'somestring' + str(someinteger)

答案 1 :(得分:0)

试试这个:

def main():
    airfareCost = int(input ("Airfare Cost"))
    hotelCost = int(input ("Hotel Cost"))
    mealsCost = int(input ("Meals Cost"))
    Total = int(airfareCost + hotelCost + mealsCost)
    print "Mini-vacation time!"
    print "Below lists the expenses for your trip to the Florida Keys.\n"
    print "  Item                Cost"
    print "  Airfare:            ${}".format(airfareCost)
    print "  Hotel:              ${}".format(hotelCost)
    print "  Meals:              ${}".format(mealsCost)
    print "  ---------------------------"
    print "  Total:              ${}".format(Total)
    print "  Have a fantastic trip!"