#🔒 Noob design question
137 messages · Page 1 of 1 (latest)
@grand sequoia
Remember to:
- Ask your Python question, not if you can ask or if there's an expert who can help.
- Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
- Explain what you expect to happen and what actually happens.
:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.
Not quite sure I follow what you're trying to do?
oh I see the task
what is cash? A list?
that wouldn't make sense
why ?
because a dictionary wouldn't make sense for holding all the change in a till
bruh its just a task lol
ahh it's a count of each currency
im more concerened on how to read it
ok, sorry, that does make sense
so i loop through every key right ?
Yes, and the value of that key is how many of that coin there is
so cash is my dictionary and money is every key in my dictionary?
def till_addition(cash):
count = 0
for money in cash:
yup!
So you first need to get how much that coin is worth
do you have some sort of dictionary set up for the value of a coin?
Unless they expect you to use a bunch of if/elif for the coin type, I'd consider setting up a dict like that
hmm
then ?
i havent done it this way before, youd have to talk me through it
ah actually let me try something
Or an if statement
Multiply the value of the key by 0.01 if p in key, else multiply by 1
we should hope that all coins in the till are valid coins
i was thinking
So something like:
for each key
If P in key: set multiplier to 0.01 else 1
Remove £ and p from key
New key * count * multiplier
Add to some grand sum
True
ahh I see what you're saying
if "p" in key
yeah
also ideally you would keep your coins in int, so you would multiply pounds by 100, and pence by 1
Ah yes
New key = int(key with £ and p removed)
Possible but
- less code
- faster computes
And that’s ideally something you’d wanna keep to as a programmer
not sure what you mean by translate or filter
Your solution of using a translation dictionary
ahh yeah
Where 1p gets translated to a value of 0.01
I do think your solution of removing the p or £ would be ideal
def till_addition(cash):
count = 0
for money in cash:
count += money["1p"].get(money) * 0.01
would this work ?
wouldnt .get get me the value of 1p ?
that means how many 1p there is ?
you're on the right track but the syntax isn't correct
did you create a dictionary for the coin values?
the if statement, is that important? because the key will always be there, the value can be 0
no
i didnt go that route, but i can
you want to go the route of removing the p or £ from the value?
why remove when i can just do
10p = 0.1
so you want an if/elif for each coin?
'coin_values = {
'1p': 0.01,
'2p': 0.02,
'5p': 0.05,
'10p': 0.10,
'20p': 0.20,
'50p': 0.50,
'£1' : 1,
'£2' : 2,
'£5' : 5,
'£10' : 10,
'£20' : 20,
'£50' : 50,
}
okay i did th e dict
what now
ok, so you can stick to this setup now
since the keys of cash exist within the dict of coin_values, you can look up the value of each coin
coin_worth = coin_values[money]
we haven't multiplied it yet
we're breaking it into steps
don't try and do everything in one line
def till_addition(cash):
count = 0
for money in cash:
coin_worth = coin_values[money]
coin_values = {
'1p': 0.01,
'2p': 0.02,
'5p': 0.05,
'10p': 0.10,
'20p': 0.20,
'50p': 0.50,
'£1' : 1,
'£2' : 2,
'£5' : 5,
'£10' : 10,
'£20' : 20,
'£50' : 50,
}
this is what i have
Let's rename some variables here so it's a bit more clear
coin_values = {
'1p': 0.01,
'2p': 0.02,
'5p': 0.05,
'10p': 0.10,
'20p': 0.20,
'50p': 0.50,
'£1' : 1,
'£2' : 2,
'£5' : 5,
'£10' : 10,
'£20' : 20,
'£50' : 50,
}
def till_addition(cash):
total_amount = 0
for coin in cash:
coin_worth = coin_values[coin]
I would also define the dict above the function for good code structure
!e
coin_values = {
'1p': 0.01,
'2p': 0.02,
'5p': 0.05,
'10p': 0.10,
'20p': 0.20,
'50p': 0.50,
'£1' : 1,
'£2' : 2,
'£5' : 5,
'£10' : 10,
'£20' : 20,
'£50' : 50,
}
def till_addition(cash):
total_amount = 0
for coin in cash:
coin_worth = coin_values[coin]
print(coin_worth)
cash = {'1p':3, '5p':2, '50p':5}
till_addition(cash)
@green sable :white_check_mark: Your 3.12 eval job has completed with return code 0.
001 | 0.01
002 | 0.05
003 | 0.5
I'd also give yourself some sample data to work with so you can test as you go
Does this output make sense to you?
this shouldnt 0.01 have 3 ?
i get this apart from
we're just looking at the value of each right now. We aren't yet taking into account how many of each coin there is yet
does coin_value[coin] get you the values of each key?
Yes
okay thanks
!e
coin_values = {
'1p': 0.01,
'2p': 0.02,
'5p': 0.05,
'10p': 0.10,
'20p': 0.20,
'50p': 0.50,
'£1' : 1,
'£2' : 2,
'£5' : 5,
'£10' : 10,
'£20' : 20,
'£50' : 50,
}
def till_addition(cash):
total_amount = 0
for coin in cash:
coin_worth = coin_values[coin]
print(f'The value of {coin} is {coin_worth}')
cash = {'1p':3, '5p':2, '50p':5}
till_addition(cash)
@green sable :white_check_mark: Your 3.12 eval job has completed with return code 0.
001 | The value of 1p is 0.01
002 | The value of 5p is 0.05
003 | The value of 50p is 0.5
we can add some prints to really make it clear that we're getting the correct data
def till_addition(cash):
coin_worth = 0
for coin in cash:
coin_worth += cash[coin] * coin_values[coin]
how does this look now ?
give it a go. Add a print so you can see the outcome
i'll create a test
You guys seem to be on the right track so will leave you to it.
@green sable sent you a dm on what I think the solution should be
you aren't printing anything
you'll never see an output without print
you also aren't calling the function
when I refer to testing, I'm just saying running the code and seeing if the output was what you expected
this was a test
it wasn't the final answer, but you should absolutely be testing your code along the way
by breaking the logic into small parts and testing them individually, you'll have a much better understanding of what your code is doing when it runs
old files
then I'm not sure what you're trying to do
bruh
its because line 3 i wrote
test_test
aha it did pass it. its the 3rd one here.
i was expecting it to be at the bottom
awesome thank you @green sable
np!
elaborate on what ur tryna do
This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.