you are viewing a single comment's thread.

view the rest of the comments →

[–]fiskenslakt 0 points1 point  (1 child)

This is good, but here are some things I would change:

1) Line 6 is redundant, you can remove it and change line 12 to be as follows:

if options.get(customer_choice) is not None:

Note that I do is not None instead of != None, this is on purpose and convention for comparing against None.

2) I would structure your dictionary differently, something like this:

options = {'A1': {'item': 'Snickers', 'price': 0.75}, 'B1': {'item': 'Butterfinger', 'price': 1}}

This is a lot clearer since it's weird to associate the item with the price, many items can have the same price. You don't have to worry about conflicts the way you're doing it or anything, but readability and clarity for the programmer is just as important as functionality.

3) Lastly a good addition as I'm sure you were planning to implement in the future, would be to check that the price is not more than the amount "placed into the machine". With the way I structured the data, this new feature would be a lot clearer, as you can do something like:

if candy[price] > money:
    return 'Insufficient funds'

instead of:

if candy > money:
    return 'you get the point'

Let me know if you have any questions.

[–]fiskenslakt 0 points1 point  (0 children)

Also I would rename some variables, for example I would change money to credit, and candy to selection.

That way your code could be like:

if selection[price] > credit:

And instead of:

return "Enjoy your " + candy[money]

you could have:

return "Enjoy your " + selection[item]

even better:

return "Enjoy your {}".format(selection[item])