1

I have the following code:

Full code listing: https://repl.it/JSUN/1

I am looking, for teaching purposes, to obtaining the best method/solution for two things:

Q1. working with currency variables, such as the ones stored in the basket list below

basket=[['Iphone', '£900'], ['Samsung', '£1020'], ['Toshiba', '£700']]

In the checkout sub, the intention is to add the COSTS for the corresponding item together.

So if the basket contained the items above, the required output would be £900+£1020+£700 =£2620

I have tried various things like converting to integer, which obviously doesn't work and I imagine that some sort of string manipulation may be the only way forward (which seems unnecessarily complex). A language like VB.Net has a currency data type which would make this task considerably simpler. What would be the pythonic way of solving this?

Q2. For looping through the entire basket to produce all items in the basket on checkout, and not just the first 2

I tried this, which didn't work:

  for items in basket:
    print("Your total bill is:", basket[items][1])
    items=items+1

and this, also erroneous:

for i in len(basket):
    print("Your total bill is:", basket[i][1])

Error:

TypeError: 'int' object is not iterable

Both questions are linked, as they both pertain to the checkout() sub, in which the addition of all the currency variables in the basket list is the objective!

Compoot
  • 2,227
  • 6
  • 31
  • 63
  • 1
    Have you had a look at https://code.google.com/archive/p/python-money/ for the first part of your question? – havanagrawal Jul 07 '17 at 18:39
  • Thank you - that's a useful comment. However, I'm using this for teaching purposes, so really just need to know the easiest method of completing the task (not for commercial or any other purpose) Thanks! – Compoot Jul 07 '17 at 18:40

1 Answers1

3

Q1:

Given that it is only for a basic teaching purpose, you could just strip out the pound sign (assuming consistent representation):

def extract_cost(money_in_pounds):
    return int(money_in_pounds[1:])

basket = [['Iphone', '£900'], ['Samsung', '£1020'], ['Toshiba', '£700']]
total_cost = 0
for items in basket:
    total_cost = total_cost + extract_cost(items[1])

print(total_cost)

Without writing another function

basket = [['Iphone', '£900'], ['Samsung', '£1020'], ['Toshiba', '£700']]
total_cost = 0
for items in basket:
    # items[1] selects the cost
    # items[1][1:] selects the sub-string of that cost from the 1st index to the end, i.e. remove the currency notation
    # int() then converts it into an integer
    cost_in_pounds = int(items[1][1:])
    total_cost = total_cost + cost_in_pounds

print(total_cost)

More concise code (Suggested by Jon Clements below)

total_cost = sum(int(cost[1:]) for item_name, cost in basket)

Q2:

items is not the index, it is the content of the list, so items itself is the inner list:

for items in basket:
    print("Your total bill is:", items[1])
havanagrawal
  • 1,039
  • 6
  • 12
  • 2
    Assuming a currency symbol is always present and isn't mixed, then: `total_cost = sum(int(cost[1:]) for item, cost in basket)` works nicely – Jon Clements Jul 07 '17 at 18:46
  • Fantastic! This works - Upvote! However, is this is the simplest way to approach the answer - by using another sub? Possibly...but I'll give it a little time before accepting your brilliant response in case anyone else has anything to add/improve on. Thank you – Compoot Jul 07 '17 at 18:49
  • Ideally, I'd like something that does not use another sub? If it is not too much trouble, are you able to add this to your answer as a alternative as well. (as mentioned it is for BASIC teaching, so passing parameters is still a little beyond some individuals at this stage) – Compoot Jul 07 '17 at 18:50
  • @MissComputing, What do you mean by "sub"? Do you mean to say a function? You want a solution that doesn't use another function? – havanagrawal Jul 07 '17 at 18:52
  • @Havan - yes function. I default to VB terminology. Yes, I mean function ...although arguably, a function is also a subroutine, but of course not all subroutines are functions!? But back to the question -my preference is for a solution that does not make use of another function and the code is in the checkout function itself – Compoot Jul 07 '17 at 18:52
  • I'd also appreciate a comment/discussion regarding whether or not a function is also a subroutine. I want to be accurate in my descriptions! – Compoot Jul 07 '17 at 19:10
  • All solved and perfect, thanks! Except - let me know about my usage of the term sub and function interchangeably. I know a function returns a single value where as a subroutine does not ALWAYS need to do this. This suggests that a subroutine can also be a function, although not all subroutines are functions, some are!? – Compoot Jul 07 '17 at 19:13
  • @MissComputing I believe subroutine and function to be interchangeable terms, and only depends on the context in which you use them. When I am talking about Perl code, my mind almost automatically switches to calling stuff subroutines, but when talking about Python, I call it functions. As a further (perhaps controversial point), I consider "methods" to be functions bound to classes. – havanagrawal Jul 07 '17 at 19:16
  • Some reference links: https://stackoverflow.com/questions/6048344/what-is-the-difference-between-a-function-and-a-subroutine https://stackoverflow.com/questions/155609/difference-between-a-method-and-a-function – havanagrawal Jul 07 '17 at 19:16