• Welcome to Smashboards, the world's largest Super Smash Brothers community! Over 250,000 Smash Bros. fans from around the world have come to discuss these great games in over 19 million posts!

    You are currently viewing our boards as a visitor. Click here to sign up right now and start on your path in the Smash community!

I need help with Python Dictionaries

Vyse

Faith, Hope, Love, Luck
BRoomer
Joined
Jul 6, 2005
Messages
9,561
Location
Brisbane, Australia
Seeing as as few of you guys are familliar with Python, I'd like to ask for some assistance because I'm seriously stuck =_=

Basically I'm trying to populate two dictionaries.
Before I had separate blocks of code to populate each dictionary
(One was an English to German document and the other was German to English)

In the documents being called they are separated by a tabbed space within the document, though, I haven't used the CSV to identify the individual words on each row.

Anyway:


I tried getting it to return the German translation of 'Dog', but that error comes up.
For the life of me I can't figure out what to do :ohwell:

If I can't figure it out soon, I'll just have to submit it. At least it works the way I had it, though I'll lose marks in efficiency.





Edit: Nevermind, I figured it out



Just one of those programmer's block things I guess.
I'll leave it up just in case somebody feels like critiquing my lame programming skillz lol.
 

Big Sean

Smash Journeyman
Joined
Jun 3, 2003
Messages
484
Location
Berkeley
oh man that was the most obfuscated python code i've ever seen. It took me forever to figure out what you where doing. Okay here's how you really do this :)
HTML:
def Dictionary(txtdoc, dict_name):  # note uppercamel case is discouraged for anything but Class names.
     file = open(txtdoc, 'r')  
     for line in file.readlines(): 
         for a, b in line.split(): 
             dict_name[a] = b 
     file.close()
Okay man I have to say that your code pretty much works by accident. First you don't even use the for loop. You iterate through the line with a self made iterator i, and use a break to leave. This exactly what a While True loop should be used for. Secondly you declare WordLen = len(Astring) which is cool, I can see where your with this. Then you say "for WordLen in Astring:" and I can again see what you meant but python interpreted it in a completely different way than what you thought. Python creates a new local variable WordLen that masks the last variable WordLen meaning that this line WordLen = len(Astring) becomes completely useless. for loops in python take an iterable (think array) and assign a value from the iterable to the local variable. So if your string line looks like "hello guttentag" WordLen is first assigned the value 'h', then 'e', then 'l', then 'l' etc ... because a string in python is treated as an iterable of letters. This is cool if you want is to iterate through letters, but I can see that your code is more interested in numbers than letters. What you wanted is to iterate so that you that your iterable is [0, 1, 2, 3, 4, ......, WordLen - 1]. Python provides a special function for this called range. given a single number as an argument it returns an enumerated list of numbers from 0 to the number - 1. The pythonic way of doing this then is "for i in range(WordLen):". In python if your code looks too complicated then it probably is.
 

Vyse

Faith, Hope, Love, Luck
BRoomer
Joined
Jul 6, 2005
Messages
9,561
Location
Brisbane, Australia
Yeah, I very quickly realised that my code is waaaaaaaaaaaaaaay overly complicated.
I managed to cut out at least a third of the code before I handed it in, but it looks like I could've made it ALOT more efficient :p

Thank you very much for the advice though!
:)
 
Top Bottom