Python Practice Sample:
Given the starter code:
ITEM_COST = 302.48
TAX_RATE = 0.06
QUANTITY = 13
NAME = "goodie"
CONTAINER = "bag"
Write a loop that prints the output formatted as below:
$ 320.63 goodie01bag
$ 641.26 goodie02bag
$ 961.89 goodie03bag
$ 1,282.52 goodie04bag
$ 1,603.14 goodie05bag
$ 1,923.77 goodie06bag
$ 2,244.40 goodie07bag
$ 2,565.03 goodie08bag
$ 2,885.66 goodie09bag
$ 3,206.29 goodie10bag
$ 3,526.92 goodie11bag
$ 3,847.55 goodie12bag
Note that the item name is formed by concatenating NAME followed by the string representation of a number between 1 and 12 followed by the CONTAINER. The string representation of the number is preceded by a 0 if the number is less than 10. The cost is the item cost multiplied by the corresponding quantity (1 through 12) multiplied by (1 + TAX RATE) The value is formatted right justified in a field of 9 with 2 after the decimal, and commas for values > 1000. A tab character separates the two values in each line. The amounts are right justified.
Code:
ITEM_COST = 302.48 # given Cost
TAX_RATE = 0.06 # tax rate
QUANTITY = 13 # quantity
NAME = "goodie" # name
CONTAINER = "bag" # container
for i in range(1,QUANTITY): # for loop to find the result
ITEM_NAME = NAME+str(i).rjust(2,'0')+CONTAINER # creating the name
TOTAL_COST = ITEM_COST * (i) * (1+TAX_RATE) # calculating total cost
COMMA_FORMAT = "{0:,.2f}".format(TOTAL_COST) # formatting the Value
print('$'+str(COMMA_FORMAT).rjust(9,' ')+'\t'+ITEM_NAME ) # printing the result in given format with rjust of 9
Sample output:
Note: Please let me know if you have any doubt.
Get Answers For Free
Most questions answered within 1 hours.