Ask your own question, for FREE!
MIT 6.00 Intro Computer Science (OCW) 8 Online
OpenStudy (anonymous):

In problem set 8 it asks you to take a file of strings, convert each string into a list of ['name',value,hours], and make a library mapping each 'name' to a tuple of (value,hours). the names are number strings (like 2.00,2.01, etc), and for some reason every time I try to map a number string to a value-hours tuple, the number string gets rounded down in the library, eg: library = {} list = ['2.01',3,4,] library[list[0]] = tuple(list[1:]) should print a library of {'2.01':(3,4)} but instead it gives me {'2':(3,4)} what is happening here?

ganeshie8 (ganeshie8):

``` >>> library = {} >>> list = ['2.01',3,4,] >>> library[list[0]] = tuple(list[1:]) >>> print library {'2.01': (3, 4)} ``` it prints correctly for me

OpenStudy (anonymous):

Did you get that to work? Your code looks correct. If your're still having trouble use pastebin.com to post your entire program so I can have a look and I'll help if I can

OpenStudy (anonymous):

ya I'm still having trouble with this. maybe it has something to do with the file I am extracting these lists from, but if it is I have no idea how to fix it. I have posted the full code on pastebin.com under "ps8 file to dictionary". any insight would be much appreciated!

OpenStudy (anonymous):

http://pastebin.com/1zDZHKT6

OpenStudy (anonymous):

you have plenty of print statements in there, when does it start going wrong? take a look at this and see if yours is doing the same thing http://dpaste.com/1111543/

OpenStudy (anonymous):

thanks bwCA your version actually works. I do not understand why mine doesn't work though. everything is fine until the last print statement which displays the library. instead of adding pairs to the library, it rounds the value of the key string, thus causing it to change the value of the first key rather than adding a new pair.

OpenStudy (anonymous):

the difference I see between your program and mine is that I convert each line of the file to a list first, then into a library pair. could that have something to do with it?

OpenStudy (anonymous):

at line 48 you are using item[0] as the key. item is a string. you are picking the first character of the string for your key - http://dpaste.com/1123649/

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!