In: Computer Science
So previously in my class I had to write a python program where the user would enter the price for 5 different items, and it would calculate the subtotal, total tax owed and the total amount owed. Now I need to write a program that would do the same but would be done using modules in the code, I am having a hard time figuring modules out. Below is the code from the original program not using modules:
# Enter price for Item1
item1 = float(input('Enter the price for item1: '))
# Enter price for Item2
item2 = float(input('Enter the price for item2: '))
# Enter price for Item3
item3 = float(input('Enter the price for item3: '))
# Enter price for Item4
item4 = float(input('Enter the price for item4: '))
# Enter price for Item5
item5 = float(input('Enter the price for item5: '))
print()
# Calculate the total owed before tax
subTotal = item1 + item2 + item3 + item4 + item5
print('The subtotal of the items is: ', subTotal)
# Calculate the total tax amount owed
totalTax = subTotal * .06
print('The total tax owed is: ', totalTax)
# Calculate the total amount owed
totalAmount = subTotal + totalTax
print('The total amount owed is: ', totalAmount)
def getSum(item1, item2, item3, item4, item5): return item1 + item2 + item3 + item4 + item5 def getTotalTax(subtotal): return subTotal * .06 def getTotalAmount(subTotal, totalTax): return subTotal + totalTax def main(): # Enter price for Item1 item1 = float(input('Enter the price for item1: ')) # Enter price for Item2 item2 = float(input('Enter the price for item2: ')) # Enter price for Item3 item3 = float(input('Enter the price for item3: ')) # Enter price for Item4 item4 = float(input('Enter the price for item4: ')) # Enter price for Item5 item5 = float(input('Enter the price for item5: ')) print() # Calculate the total owed before tax subTotal = getSum(item1,item2,item3,item4,item5) print('The subtotal of the items is: ', subTotal) # Calculate the total tax amount owed totalTax = getTotalTax(subTotal) print('The total tax owed is: ', totalTax) # Calculate the total amount owed totalAmount = getTotalAmount(subTotal, totalTax) print('The total amount owed is: ', totalAmount) main()