[Tutor] I need help with my project

Cameron Simpson cs at cskk.id.au
Fri Nov 30 18:14:02 EST 2018


Avi and Alan and Sibylle, you're making this a bit hard on the OP 
(Treyton).

Yes he's supplied no context, but it is easy to make some suggestions.  
Each of yours suggests he design a much wider system (menu entry, web 
interface, some kind of GUI). All of which is (a) beyond him and (b) 
irrelevant.

Why not pretend he _has_ the existing order, from wherever.

Suggest ways to store that order (in a list, or a dict mapping ordable 
items to counts, or something). Then ask him to write a little Python, 
or even detailed English prose.

Treyton: you seem to have recitied a homework question:
>If the user selected a sandwich, french fries, and a beverage, reduce 
>the
>total cost of the order by $1.00.
>This is what I have to do and I don't know where to start.

Ok, this is clear: Treyton can't get off the ground, very common for 
beginning programmers.

The core challenge is to break your problem into a sequence of tasks.  
How would _you_, a person, do this if you had a food order given to you?

Think about a food order. It is usually a list of standard food items, a 
count of how many of each. And each item will have a cost.

The total cost is the sum of (each item's cost * its price * its count), 
for each item in the order. Or for all possible items, by presuming that 
unordered items just have a count of 0.

So you need:

A label for each item, so you can talk about it. You can just use a 
string for this, eg "sandwich" or "fries". Make the strings simple to 
start with to avoid spelling mistakes. You can always associate better 
names with the short strings later.

You need a table of items and their costs. It is normal to make a 
mapping for this, such a Python's dict type. You can write dicts 
literally:

  costs = {
    "sandwich": 200,
    "fries": 100,
  }

In the example above, I'm imagining you have dollars and cents, and 
making prices in cents.

You also need a representation of the order, being the item type and the 
count. You could use a Python list for this. Example:

  [ "fries", 2 ]

The whole order might be a list of those, example:

  [ ["fries", 2 ], [ "sandwich", 3 ] ]

So, a list of lists.

For purposes of your program you can just set all this stuff up at the 
beginning, not worrying about GUIs or input forma or any complication.

  whole_order = [
    ["fries", 2 ],
    [ "sandwich", 3 ]
  ]

Now comes the part you need to do:

- write some Python code to compute the total cost of the order (item 
  cost * item count), summed for all the items. Print this raw total so 
  that you can see it is correct.

- write some totally separate code to look at the order and decide if 
  the client met your special condition (sandwich, fries, beverage) and 
  get a true/false result. Print this, too.

- write a Python statement to subtract $1.00 (or 100 cents) from the 
  total if that condition is true. Print that.

Then fiddle the order and run your programme several times to check that 
it is behaving the way it should.

If you find difficulties you cannot surmount, come back here (by 
replying directly to one of the messages in your discussion) with:

- your complete code

- your expected output, and the output from your programme

- a complete transcript of any error message, for example if your 
  programme raised an exception

Make sure these are inline in your message, _not_ attachments. We drop 
attachments in this list.

Cheers,
Cameron Simpson <cs at cskk.id.au>


More information about the Tutor mailing list