Tyler
Tyler

Reputation: 3

Adding 3 costs together to form a total using python

I'm fairly new to python and I'm taking a bunch of inputs with as if it were a receipt for mini vacation and trying to add all three categories up to make a total. I keep getting the cannot concatenate 'str' and 'int' objects problem that I can't seem to get around no matter what I do.

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()

Upvotes: 0

Views: 298

Answers (2)

Jossildur
Jossildur

Reputation: 11

You need to explicitly cast the integer values to strings.

'somestring' + str(someinteger)

Upvotes: 1

Asav Patel
Asav Patel

Reputation: 1162

Try this:

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!"

Upvotes: 0

Related Questions