Python

Discussion in 'Mac Programming' started by Geodripp, Nov 29, 2009.

  1. Geodripp macrumors newbie

    Joined:
    Nov 29, 2009
    #1
    #I'm taking this intro to Python class and I cant seem to keep it down. Right now I'm having trouble passing values and I was #wondering if anyone could help me out.

    #This is what I'm working on

    Code:
    DISCOUNT_RATE = 0.05
    #
    SALES_TAX_RATE = 0.06
    def display_message():
        print"""
    ___________________________
    |Five values are displayed|
    |                         |
    |Item total               |
    |Discount amount          |
    |Amount due after discount| 
    |Sales tax                |
    |And                      |
    |Total amount due         |
    ---------------------------
        """
    display_message()
    def main():
        unit_price = input ("Input the products price: ")
        number_of_units = input ("Input number of products being purchased: ")
        item_total = unit_price * number_of_units
        discount_amount = item_total * DISCOUNT_RATE
        total_after_discount = item_total - discount_amount
        sales_tax_due = total_after_discount * SALES_TAX_RATE
        total_amount_due = total_after_discount + sales_tax_due
    def display_results():
        print
        print "__________________________________"
        print "Item total:           ",          '$%3.2f' %item_total, "|"
        print "----------------------------------"
        print
        print "__________________________________"
        print "Discount amount:      ",     '$%3.2f' %discount_amount, "|"
        print "----------------------------------"
        print
        print "__________________________________"
        print "Total after discount: ",'$%3.2f' %total_after_discount, "|"
        print "----------------------------------"
        print
        print "__________________________________"
        print "Sales tax due:        ",       '$%3.2f' %sales_tax_due, "|"
        print "----------------------------------"
        print
        print "__________________________________"
        print "Total amount due:     ",    '$%3.2f' %total_amount_due, "|"
        print "----------------------------------"
    
    
    main()
    display_results()
    #Do I want to put item_total in display_results() like (display_results(item_total)) or am I way off?
     
  2. Geodripp thread starter macrumors newbie

    Joined:
    Nov 29, 2009
    #2
    Oh also I do need the three functions it was specified in the assignment.

    I can get it to work fine in one function so the value passing is what I think Im having problems with. Can anyone help me with this?
     
  3. skunkworker macrumors regular

    Joined:
    Sep 9, 2007
    #3
    since you will always be printing the results I put it in the end of main().

    Code:
    #! /usr/bin/python
    
    DISCOUNT_RATE = 0.05
    SALES_TAX_RATE = 0.06
    
    def display_message():
        print"""
    ___________________________
    |Five values are displayed|
    |                         |
    |Item total               |
    |Discount amount          |
    |Amount due after discount| 
    |Sales tax                |
    |And                      |
    |Total amount due         |
    ---------------------------
        """
    display_message()
    def main():
        unit_price = input ("Input the products price: ")
        number_of_units = input ("Input number of products being purchased: ")
        item_total = unit_price * number_of_units
        discount_amount = item_total * DISCOUNT_RATE
        total_after_discount = item_total - discount_amount
        sales_tax_due = total_after_discount * SALES_TAX_RATE
        total_amount_due = total_after_discount + sales_tax_due
        display_results(item_total,discount_amount,total_after_discount,sales_tax_due,total_amount_due)
    
    def display_results(item_total,discount_amount,total_after_discount,sales_tax_due,total_amount_due):
        print
        print "__________________________________"
        print "Item total:           ",          '$%3.2f' %item_total, "|"
        print "----------------------------------"
        print
        print "__________________________________"
        print "Discount amount:      ",     '$%3.2f' %discount_amount, "|"
        print "----------------------------------"
        print
        print "__________________________________"
        print "Total after discount: ",'$%3.2f' %total_after_discount, "|"
        print "----------------------------------"
        print
        print "__________________________________"
        print "Sales tax due:        ",       '$%3.2f' %sales_tax_due, "|"
        print "----------------------------------"
        print
        print "__________________________________"
        print "Total amount due:     ",    '$%3.2f' %total_amount_due, "|"
        print "----------------------------------"
    
    main()
    What do you mean by three functions?
    something like
    main() compute() and show results() ?

    if so then this might do the trick.
    Code:
    #! /usr/bin/python
    
    DISCOUNT_RATE = 0.05
    SALES_TAX_RATE = 0.06
    
    def display_message():
        print"""
    ___________________________
    |Five values are displayed|
    |                         |
    |Item total               |
    |Discount amount          |
    |Amount due after discount| 
    |Sales tax                |
    |And                      |
    |Total amount due         |
    ---------------------------
        """
    display_message()
    
    
    def compute(unit_price,number_of_units):
        item_total = unit_price * number_of_units
        discount_amount = item_total * DISCOUNT_RATE
        total_after_discount = item_total - discount_amount
        sales_tax_due = total_after_discount * SALES_TAX_RATE
        total_amount_due = total_after_discount + sales_tax_due
        display_results(item_total,discount_amount,total_after_discount,sales_tax_due,total_amount_due)  
    
    def display_results(item_total,discount_amount,total_after_discount,sales_tax_due,total_amount_due):
        print
        print "__________________________________"
        print "Item total:           ",          '$%3.2f' %item_total, "|"
        print "----------------------------------"
        print
        print "__________________________________"
        print "Discount amount:      ",     '$%3.2f' %discount_amount, "|"
        print "----------------------------------"
        print
        print "__________________________________"
        print "Total after discount: ",'$%3.2f' %total_after_discount, "|"
        print "----------------------------------"
        print
        print "__________________________________"
        print "Sales tax due:        ",       '$%3.2f' %sales_tax_due, "|"
        print "----------------------------------"
        print
        print "__________________________________"
        print "Total amount due:     ",    '$%3.2f' %total_amount_due, "|"
        print "----------------------------------"
    
    def main():
        unit_price = input ("Input the products price: ")
        number_of_units = input ("Input number of products being purchased: ")
        compute(unit_price,number_of_units)
    
    main()
     
  4. Geodripp thread starter macrumors newbie

    Joined:
    Nov 29, 2009
    #4
    Yeah I actually worked on it a bit more and got it.

    Thanks for the help though I really do appreciate it. :)
     

Share This Page