#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    3
    Rep Power
    0

    Question I'm a python beginner, I need a answer for this python question!


    so our teacher gave us bunches of practice questions for upcoming exams. This is number one of the questions. I don't know how to do it and i am a beginner :rockon: .


    This is the question

    Joe’s Electronics is having a sale for Boxing day. If a customer spends over $300 and buys at least 6 items, your cheapest item will be free. Write a program that will prompt for the prices of items until the user enters the value 0. If the user enters an invalid amount (a negative $ amount) the program will output an error message and ask the user to enter a value again. This program will determine and output the total number of items, subtotal, discount amount (if applicable), total after discount, the total sales tax, and the final cost.



    Sample Run
    Enter a price: 100
    Enter a price: -100
    Sorry, -100 is invalid
    Enter a price: 100
    Enter a price: 100
    Enter a price: 100
    Enter a price: 100
    Enter a price: 100
    Enter a price: 0

    Total number of items purchased: 6
    Subtotal: $600
    Discount :$100
    Total less Discount: $500
    HST: $65
    Final Cost: $565
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,931
    Rep Power
    481
    Gentle student, please post a programming effort or at least the outline of an algorithm.

    How would you decide if the customer is eligible for discount? Specifically how would you find the subtotal, and how would you know how many items are purchased? How would you find the lowest cost item? How would you eliminate it from the sum?

    How would you read user responses to your questions? How would you detect errors? Please show some sort of programming effort. Please demonstrate that you've actually tried to use the python interpreter.

    Thank you, Dave.
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    3
    Rep Power
    0

    Im trying


    Originally Posted by b49P23TIvg
    Gentle student, please post a programming effort or at least the outline of an algorithm.

    How would you decide if the customer is eligible for discount? Specifically how would you find the subtotal, and how would you know how many items are purchased? How would you find the lowest cost item? How would you eliminate it from the sum?

    How would you read user responses to your questions? How would you detect errors? Please show some sort of programming effort. Please demonstrate that you've actually tried to use the python interpreter.

    Thank you, Dave.
    Yes I have tried the question. This is what I have done. But it isnt working.

    items = 0 # The item
    cost = 0 #The price of the item
    total = 0 #The total of the purchases
    subt = 0 #The subtotal of the purchases
    lowest = 0 #The lowest price
    dct = 0 #The discount
    hst = 0 #The tax
    item = 0 #The items

    print "\t\t Joes Electronics"
    print "\t\t Boxing Day Sale 2012"

    while cost!=0:
    cost = int(raw_input("\nEnter the price of your item:$"))
    if cost>0:
    item=item+1
    subt = subt+cost
    if cost<lowest:
    lowest = cost
    if items>6:
    subt=sbt-lowest
    dct=lowest
    hst = subt*hst

    total = subt+hst-dct
    print "\nThe discount is",dct,"dollars"
    print "\nThe number of items bought is",items,"dollars"
    print "\nThe subtotal is",subt,"dollars"
    print "\nThe tax is",hst,"dollars"
    print "\nThe total is",total,"dollars"
  6. #4
  7. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,931
    Rep Power
    481
    Thank you, that gives us something to work with.

    You had initialized cost to 0. Then you wrote
    while cost != 0:
    and so the block in the while loop never executed.

    I think you had other confusion about item or items. Choose 1, unless you had a purpose I didn't comprehend.

    lowest started at 0. All other items are necessarily larger than that, and so lowest remain at 0.
    To find the lowest you need to start with an item of the list or from a large number. Infinity if it's available in your programming language.

    Likewise, to find the biggest you need to start comparing with an item of the list or with a small number, negative infinity if permitted.

    Otherwise, your logic wasn't all together correct but it was a good try. Spoiler:
    Code:
    items = 0 # The item
    cost = 0 #The price of the item
    total = 0 #The total of the purchases
    subt = 0 #The subtotal of the purchases
    lowest = 99999999999999                   #The lowest price must start high
    dct = 0 #The discount
    tax_rate = 0.13
    hst = 0 #The tax
    
    print "\t\t Joes Electronics"
    print "\t\t Boxing Day Sale 2012"
    
    while True:
        s = raw_input("\nEnter the price of your item:$")
        try:                          # let's trap invalid numbers as well
            cost = int(s)           # suppose the input is xxyt or 23.9e12
        except:
            cost = -1
        if cost < 0:                        # restart the loop
            print('Sorry!  %s is invalid.'%s)
            continue
        if 0 == cost:                       # exit the loop
            break                                          
        items += 1
        subt += cost
        if cost<lowest:
            lowest = cost
            print('lowest updated as %d'%lowest)
    
    
    if (6 <= items) and (300 <= subt):        # if eligible
        print('discount eligible!')
        total = subt-lowest
        dct = lowest
    else:
        total = subt
        dct = 0
    
    hst = int(round(total * tax_rate))
    
    final_cost = total + hst
    
    print('Total number of items purchased: %d'%items)
    print('Subtotal: $%d'%subt)
    print('Discount: $%d'%dct)
    print('Subtotal less Discount: $%d'%total)
    print('HST: $%d'%hst)
    print('Final Cost: $%d'%final_cost)
    [code]Code tags[/code] are essential for python code and Makefiles!
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    3
    Rep Power
    0

    Talking


    Originally Posted by b49P23TIvg
    Thank you, that gives us something to work with.

    You had initialized cost to 0. Then you wrote
    while cost != 0:
    and so the block in the while loop never executed.

    I think you had other confusion about item or items. Choose 1, unless you had a purpose I didn't comprehend.

    lowest started at 0. All other items are necessarily larger than that, and so lowest remain at 0.
    To find the lowest you need to start with an item of the list or from a large number. Infinity if it's available in your programming language.

    Likewise, to find the biggest you need to start comparing with an item of the list or with a small number, negative infinity if permitted.

    Otherwise, your logic wasn't all together correct but it was a good try. Spoiler:
    Code:
    items = 0 # The item
    cost = 0 #The price of the item
    total = 0 #The total of the purchases
    subt = 0 #The subtotal of the purchases
    lowest = 99999999999999                   #The lowest price must start high
    dct = 0 #The discount
    tax_rate = 0.13
    hst = 0 #The tax
    
    print "\t\t Joes Electronics"
    print "\t\t Boxing Day Sale 2012"
    
    while True:
        s = raw_input("\nEnter the price of your item:$")
        try:                          # let's trap invalid numbers as well
            cost = int(s)           # suppose the input is xxyt or 23.9e12
        except:
            cost = -1
        if cost < 0:                        # restart the loop
            print('Sorry!  %s is invalid.'%s)
            continue
        if 0 == cost:                       # exit the loop
            break                                          
        items += 1
        subt += cost
        if cost<lowest:
            lowest = cost
            print('lowest updated as %d'%lowest)
    
    
    if (6 <= items) and (300 <= subt):        # if eligible
        print('discount eligible!')
        total = subt-lowest
        dct = lowest
    else:
        total = subt
        dct = 0
    
    hst = int(round(total * tax_rate))
    
    final_cost = total + hst
    
    print('Total number of items purchased: %d'%items)
    print('Subtotal: $%d'%subt)
    print('Discount: $%d'%dct)
    print('Subtotal less Discount: $%d'%total)
    print('HST: $%d'%hst)
    print('Final Cost: $%d'%final_cost)

    Ohhhhhhhhhhhhh! I see how I messed it up! Thank you very much for your help and effort David. Thank you!!

IMN logo majestic logo threadwatch logo seochat tools logo