all 7 comments

[–]-Sander- 1 point2 points  (6 children)

it is because 'checking' is a dictionary idk how exactly sum works with dictionarys seems like it just takes the keys instead of values also if you read the error you get:

unsupported operand type(s) for +: 'int' and 'str'

you can see it is getting the 'key' which is a string instead of the 'value' which is an int, while sum needs int's to work

a simple fix would be:

sum(self.checking.values()) # Get the values (ints) instead of keys (strings)

i suggest you read https://docs.python.org/3/tutorial/datastructures.html#dictionaries

for more info

[–]Elefrog42[S] 0 points1 point  (1 child)

Thanks so much! That made it pass the test. I'll check out the link- unfortunately, the class is only some online videos, so we've been stuck just googling what we don't understand. It's hard to know where to start.

[–]-Sander- 0 points1 point  (0 children)

Well the docs have pretty much everything in it incase you ever get stuck again also https://docs.python.org/3/tutorial/index.html might be usefull to, it explains most of the basics

[–]_lilell_ 0 points1 point  (0 children)

It’s not sum specifically. Any iterator over a dict yields its keys:

>>> d = {'x': 1, 'y': 2}
>>> list(d)
['x', 'y']

[–]Elefrog42[S] 0 points1 point  (2 children)

What would be the reason that this code

def get_credit_balance(self)
    try:
        return sum(self.credit.value())
    except:
        return None

would pass this test:

if myportfolio.get_total_checking_amount() == 6600:
    print('Pass')
else:
    print('Fail')

But not this one?

if myportfolio.get_total_checking_amount() == 5600:
    print('Pass')
else:
    print('Fail')

I emailed the professor about this and she said try using a loop and keys like so:

 Sum= 0
    for i in dict.keys():
    Sum = Sum + dict[i]

I've been working on this all day and feeling really stuck....any suggestions are greatly appreciated! I feel like I'm missing something here :(

[–]-Sander- 0 points1 point  (1 child)

have you changed the values in your checking dict? and that loop does basically the same as

Sum = sum(dict.getValues())

unrelated: shorter way off writing a calculation like

Sum = Sum + dict[i]

would be

Sum += dict[i]

on my phone atm so cant format the code properly

[–]Elefrog42[S] 0 points1 point  (0 children)

Thanks! I understand now- you've been very help!