#🔒 Noob design question

137 messages · Page 1 of 1 (latest)

grand sequoia
#

How should I design the next part?

So I Imagine I need ot iterate through every key, right? and by every key I need to then times (*) it by its value, 0.1, 0.2 etc...

Whats the best way to do this ?

sweet boughBOT
#

@grand sequoia

Python help channel opened

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.

grand sequoia
#

here's the task

green sable
#

oh I see the task

#

what is cash? A list?

grand sequoia
#

i just want hints

#

a dictionary

green sable
#

that wouldn't make sense

grand sequoia
#

why ?

green sable
#

because a dictionary wouldn't make sense for holding all the change in a till

grand sequoia
#

bruh its just a task lol

green sable
#

Can you show the full task?

#

Are there examples of expected outputs?

grand sequoia
green sable
#

ahh it's a count of each currency

grand sequoia
green sable
#

ok, sorry, that does make sense

grand sequoia
#

so i loop through every key right ?

green sable
#

Yes, and the value of that key is how many of that coin there is

grand sequoia
#

so cash is my dictionary and money is every key in my dictionary?

def till_addition(cash):
    count = 0
    for money in cash:
        
        
green sable
#

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?

green sable
#

Unless they expect you to use a bunch of if/elif for the coin type, I'd consider setting up a dict like that

green sable
#
coin_values = {
    '1p': 0.01,
    '2p': 0.02,
    ...
}
#

something along the lines of this

grand sequoia
#

then ?

#

i havent done it this way before, youd have to talk me through it

#

ah actually let me try something

marsh vapor
#

Or an if statement
Multiply the value of the key by 0.01 if p in key, else multiply by 1

green sable
#

we should hope that all coins in the till are valid coins

marsh vapor
#

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

green sable
#

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

grand sequoia
#

cant i just say

#

1p = 0.01

#

translate the values rather than filtering them

marsh vapor
#

Ah yes
New key = int(key with £ and p removed)

marsh vapor
#

And that’s ideally something you’d wanna keep to as a programmer

green sable
#

not sure what you mean by translate or filter

marsh vapor
green sable
#

ahh yeah

marsh vapor
#

Where 1p gets translated to a value of 0.01

green sable
#

I do think your solution of removing the p or £ would be ideal

grand sequoia
#

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 ?

green sable
#

you're on the right track but the syntax isn't correct

#

did you create a dictionary for the coin values?

grand sequoia
#

the if statement, is that important? because the key will always be there, the value can be 0

grand sequoia
#

i didnt go that route, but i can

green sable
#

you want to go the route of removing the p or £ from the value?

grand sequoia
#

10p = 0.1

green sable
#

so you want an if/elif for each coin?

grand sequoia
#

'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

green sable
#

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]

grand sequoia
#

i dont see how this extracts the value and multiplies it

#

😦

green sable
#

we haven't multiplied it yet

#

we're breaking it into steps

#

don't try and do everything in one line

grand sequoia
#

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

green sable
#

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)
sweet boughBOT
#

@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
green sable
#

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?

grand sequoia
grand sequoia
green sable
#

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

grand sequoia
#

does coin_value[coin] get you the values of each key?

green sable
#

Yes

grand sequoia
#

okay thanks

green sable
#

!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)
sweet boughBOT
#

@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
green sable
#

we can add some prints to really make it clear that we're getting the correct data

grand sequoia
#

i think i know now

#

we do

#

cash[coin] * coin_value[coin]

#

right ?

green sable
#

yes, exactly

#

and then add that amount to the total variable

grand sequoia
#
def till_addition(cash):
    coin_worth = 0
    for coin in cash:
        coin_worth += cash[coin] * coin_values[coin]
#

how does this look now ?

green sable
#

give it a go. Add a print so you can see the outcome

grand sequoia
#

i'll create a test

marsh vapor
#

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

grand sequoia
#

my tests arentw working

#

urghh

#

why doesnt it show

#

these are my old test

green sable
#

you aren't printing anything

#

you'll never see an output without print

#

you also aren't calling the function

grand sequoia
#

in tests we dont really call

#

the test should at least fail ?

green sable
#

when I refer to testing, I'm just saying running the code and seeing if the output was what you expected

green sable
#

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

grand sequoia
#

yess it works

#

but now i need to design tests

#

but my tests not working

green sable
#

Aren't these the tests?

grand sequoia
#

old files

green sable
#

then I'm not sure what you're trying to do

grand sequoia
#

its because line 3 i wrote

#

test_test

grand sequoia
#

i was expecting it to be at the bottom

#

awesome thank you @green sable

green sable
#

np!

noble egret
#

elaborate on what ur tryna do

sweet boughBOT
#
Python help channel closed

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.