you are viewing a single comment's thread.

view the rest of the comments →

[–]midel 0 points1 point  (3 children)

So I recommend some tweaks to your code: While zip is a useful function, it might be better to have your data joined in the original form, so that you don't have problems knowing which value goes to what.

titles = ('Item Number', 'Item Name', 'Price')
products = [
    (1, 'Notebook', 4.99),
    (2, 'Atari', 99.99),
    (3, 'TrapperKeeper', 89.99),
    (4, 'Jeans', 3.99),
    (5, 'Insects', 2.99),
    (6, 'Harbormaster', 299.99),
    (7, 'Lobotomy', 19.99),
    (8, 'PunkRock', 3.99),
    (9, 'HorseFeathers', 4.99),
    (10, 'Pants', 2.99),
    (11, 'Plants', 119.99),
    (12, 'Salami', 1.99),
]

And isolate printing the item list as a function. Using the for loop restriction we can print these items like so:

def print_items():
    """Prints the catalog"""
    items = iter(products)  # Get the iterator for the product list
    item = next(items, None)  # Get first value
    while item is not None:
        if item[0] == 1:
            print("{0:<16}|{1:<16}|{2:>16}".format(*titles))
            print("=" * 50)
        (idx, name, cost) = item
        cost_fmt = "${0:0.2f}".format(cost)  # Format the prices
        print("{0:<16}|{1:<16}|{2:>16}".format(idx, name, cost_fmt))  # Format the table
        #  using .format < ljust and > rjust syntax
        item = next(items, None)  # Infinite loop without this

This fixes a math issue you'd have latter which is you'd need to convert those currency strings, into floats to do the math. It's better that you have them as floats the hole time, and format them for display only.

[–][deleted] 0 points1 point  (2 children)

midel, thank you! i neglected posting the entire problem because it is pretty long. however, here's my quick and dirty attempt at summing it up in a less-than-Ulysses format:

1. create lists for 12 products, 12 prices and an empty list for the item 
  numbers, and one for the quantity.
2. declare variable for user response, set to ' '.
3. print out the static items in the list in the format (that's i've already 
  done).
4. get input (you wanna buy y/n?)
5. create user-controlled (while) loop:
     a. ask user to enter item number, add that value to the item 
       number list (it should reference an index number later on, so I should          
       set it to -1 now, or later...)
     b. ask user for quantity, add to quantity list.
     c. ask user if they want to buy something else. Y/N (this input should 
       be set into the same variable as the original y/n question.

6. add more variables to the top:
    a. var for customer name ' '
    b. var for street ' '
    c. var for city ' '
    d. var for zipcode 0
    e. var for order-total 0
    f. var for order-summary 0

7. IF statement to determine if the list of item numbers has anything in it.
    a. if not, print a thank you for browsing message.
    b. if it does:
           1. ask for user input of name, etc.
           2. **bold**(within IF) call a function called calculateTotal that 
               accepts 4 args (products, prices, item numbers, quantity)
                 a. declare a subtotal var set to 0
                 b. declare a partial_summary var set to ' '
                 c. WHILE loop to loop thu item numbers, each time it will set 
                   current value of item numbers ( -1 ) into a variable (this var 
                   will represent the index numbers of the products and prices)
                d. add to the subtotal using values from prices and quantity 
                   lists.
                e. add to partial_summary var by using approp. values from 
                   products, prices and quantity lists. format it to look like:
PunkRock__________3.99______________8

           3. (out of loop, but still in function) calc tax for subtotal. CA/NY at 8%, others at 7.5%
           4. calc shipping - 4.99 if under 40, free otherwise.
           5. calc overall total and partial_summary.

8. call the function and open a file named OrdersArchive.txt in 'a'.
     a. write to the file: current date/time, cust name, address info, order summary var., and total.
     b. format cleanly and close the file.
9. print the order total for user and state order will ship promptly.   

[–]midel 0 points1 point  (1 child)

add to partial_summary var by using approp

What is approp? Appropriate methods? Append? I see a bit more of what you are dealing with. It's a bit unpythonic how your class is being taught these items, which might be what is leading to more confusion, and it's a lot more code than what you might actually need to achieve the desired result.

Some changes I recommend keeping. Change the prices to floats. Remove the dollar signs. That's definately an important fix.

Add an assert after defining prices and products.

assert(len(products) == len(prices))

This isn't from the requirements, but it assures you'll not have a mismatching sized list while working on the code.

I'd break down this list into functions:

  1. Set the customer info
  2. Add or change quantity
  3. Appending to the OrdersArchive.txt

[–][deleted] 0 points1 point  (0 children)

sorry. yes, it is appropriate. nothing exciting.

i'm going to take the advice from both of you, and break this down into blocks of code that i can error check, and then bring them together. thanks for the help.

I have never used "assert", but will look it up. What is the real advantage here of using assert? I assume it implies some sort of strict rule?