def main():
#this program calculates the total price of a meal after inputting cost of food
#tip and sales tax
print ('Welcome to the Meal Calculator Program')
print()
#get the cost of food using the input function
food_cost = float (food_cost)
#calculate tip
tip_rate = input ("Tip rate used today is: ")
tip_rate = float (tip_rate)
#calulate sales tax
tax_rate = input ("Tax rate used today is: ")
tax_rate = float (tax_rate)
#calculate total cost here, using inputted values
total_cost = food_cost + (food_cost * tip_rate) + (food_cost * tax_rate)
print ('The total cost based on', format(food_cost, '.2f'),\
'food cost,', format(tip_rate, '.2f'),\
'tip rate, and', format(tax_rate, '.2f'),\
'tax rate is : \$', format(total_cost, '.2f'),sep=' ')
#calls main
main()

Essentially, I would like to know how I would go about inputting a food cost, our example is 23.45, and in the output, getting \$23.45 . Also, for tip rate, if I input .2, is there a way to get 20% in the output? Or input a percentage?

The {} in the format string are called format fields where format(obj1, obj2, ...) puts the objects it contains. It was introduced starting with Python 3.0 and later adopted by Python 2.7. It replaces the older C type % specifiers.

Just a taste ...

# new and …

## All 4 Replies

I would enter tip and tax in percent ...

def main():
# this program calculates the total price of a meal after inputting
# cost of food, percent tip and sales tax
print ('Welcome to the Meal Calculator Program:')
print()
# get the cost of food using the input function
# calculate tip
tip = float(input("Tip percent used today is: "))
# calulate sales tax
tax = float(input("Tax percent used today is: "))

#calculate total cost here, using inputted values
total_cost = food_cost + food_cost*tip/100 + food_cost* tax/100

format_str = """\
The total cost based on \${:.2f} food cost
{:.2f}% tip and {:.2f}% tax is \${:.2f}
"""
print(format_str.format(food_cost, tip, tax, total_cost))

# calls main
main()
format_str = """\
The total cost based on \${:.2f} food cost
{:.2f}% tip and {:.2f}% tax is \${:.2f}
"""
print(format_str.format(food_cost, tip, tax, total_cost))

EDIT: I thought this didn't run as is, but it did, could you explain the above code to me?

Here are some examples of the format() method

Python 3.3.1 (default, Apr 17 2013, 22:32:14)
[GCC 4.7.3] on linux
>>> "Give me {0} and {2}, said the {1} man".format("bacon", "other", "eggs")
'Give me bacon and eggs, said the other man'
>>> "Give me {0} and {2} eggs, said the {1} man".format("bacon", "other", 5)
'Give me bacon and 5 eggs, said the other man'
>>> "Give me {0} and {2:.2f} eggs, said the {1} man".format("bacon", "other", 5)
'Give me bacon and 5.00 eggs, said the other man'
>>> "Give me {} and {}, said the {} man".format("bacon", "eggs", "other")
'Give me bacon and eggs, said the other man'
>>> "Give me {} and {:.2f} eggs, said the {} man".format("bacon", 5, "other")
'Give me bacon and 5.00 eggs, said the other man'

The {} in the format string are called format fields where format(obj1, obj2, ...) puts the objects it contains. It was introduced starting with Python 3.0 and later adopted by Python 2.7. It replaces the older C type % specifiers.

Just a taste ...

# new and fancier Python output formatting ...
# string is default and does not have a type specifier
# {0} and {1} index the format fields
sf = 'the {0} jumped over the {1}!'
print(sf.format('mouse', 'moon'))
# no indexing needed if the objects in format() are in order
sf = 'the {} jumped over the {}!'
print(sf.format('mouse', 'moon'))
# or use keyword args
sf = 'the {ms} jumped over the {mn}!'
print(sf.format(ms='white mouse', mn='full moon'))

# allow 10 char spaces, left align (<) is default
print('the {0:10} jumped over the {1:10}!'.format('mouse', 'moon'))
# use right align (>)
print('the {0:>10} jumped over the {1:>10}!'.format('mouse', 'moon'))
# use fill (specify a single character)
print('the {0:{fill}>10} jumped over the {1:{fill}>10}!'.format(
'mouse', 'moon', fill='-'))

''' result ...
the mouse jumped over the moon!
the mouse jumped over the moon!
the white mouse jumped over the full moon!
the mouse      jumped over the moon      !
the      mouse jumped over the       moon!
the -----mouse jumped over the ------moon!
'''

Pretty powerful stuff ...

food_price_dict = {
'milk' : 3.67,
'butter' : 1.95,
'cheese' : 4.67
}
# print all dictionary items in a table
for food, price in sorted(food_price_dict.items()):
# food has 10 char field, price with 2 decimals
print('{0:10} costs \${1:4.2f}'.format(food, price))

print('-'*22)

# print a selected item
print('Milk costs \${milk:4.2f}'.format(**food_price_dict))

print('-'*22)

# print local variables with vars()
name = "Fred"
age = 47
print("{name} is {age} years old".format(**vars()))

print('-'*22)

# the comma denotes the thousand specifier ...
print('You just won \${:,}'.format(1234567890))

''' result ...
butter     costs \$1.95
cheese     costs \$4.67
milk       costs \$3.67
----------------------
Milk costs \$3.67
----------------------
Fred is 47 years old
----------------------
You just won \$1,234,567,890
'''
commented: nice examples +14
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.