Python Forum
Calling a function to return a list of percentages
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Calling a function to return a list of percentages
#8
without generator:
print ("This is a tax calculator. Enter in the price and your item\n"
"and the program will calculate how much it costs with tax.")
 
# Create tax function that takes price and tax_rate as inputs and returns the total price
# def calculate_tax(price, start, end, increment):
#     for n in range(start, end, increment):
#         tax_rate = n * .01
#         tax = price * tax_rate
#         total = round((price + tax), 2)
#         yield n, tax, tax_rate, total

def calculate_tax(price, tax_rate):
    return (price * tax_rate) * .01

# float is a variable type for decimals
item = str(input("Please enter an item: "))
my_price = float(input("Price of the item $"))
for rate in [5, 10, 15, 20, 25]:
    tax = calculate_tax(my_price, rate)
    print('{}% tax on a {} costing ${:.2f} is ${:.2f} for a total of ${:.2f}'
          .format(rate, item, my_price, tax, my_price + tax))
test:
Output:
This is a tax calculator. Enter in the price and your item and the program will calculate how much it costs with tax. Please enter an item: choc Price of the item $17.5 5% tax on a choc costing $17.50 is $0.88 for a total of $18.38 10% tax on a choc costing $17.50 is $1.75 for a total of $19.25 15% tax on a choc costing $17.50 is $2.62 for a total of $20.12 20% tax on a choc costing $17.50 is $3.50 for a total of $21.00 25% tax on a choc costing $17.50 is $4.38 for a total of $21.88
Reply


Messages In This Thread
RE: Calling a function to return a list of percentages - by Larz60+ - Mar-25-2017, 01:20 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  How to sort a list with duplicates and return their unique indices. Echoroom 3 3,539 Sep-23-2022, 07:53 AM
Last Post: deanhystad
  Use of function/return Paulman 6 2,387 Oct-24-2021, 11:07 PM
Last Post: Paulman
  Multiple return from function Grimmar 7 3,591 Mar-22-2021, 09:20 PM
Last Post: Grimmar
  Lambda function not return value mbilalshafiq 4 3,338 Jul-04-2020, 07:47 AM
Last Post: ndc85430
  Child class function of Log return "None" mbilalshafiq 2 2,241 Jun-30-2020, 07:22 PM
Last Post: mbilalshafiq
  Return the sum of the first n numbers in the list. pav1983 3 4,155 Jun-24-2020, 03:37 AM
Last Post: deanhystad
  Question on "define function"; difference between return and print extricate 10 4,753 Jun-09-2020, 08:56 PM
Last Post: jefsummers
  [split] problem with function return value ops 1 3,368 Apr-13-2020, 01:48 PM
Last Post: buran
  Function to return today's month, day, and year sbabu 9 4,966 Jan-28-2020, 06:20 PM
Last Post: snippsat
  Calling a function from another function johneven 2 2,891 Jul-09-2019, 12:42 AM
Last Post: micseydel

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020