Need help with for loops

A forum for general discussion of the Python programming language.

Need help with for loops

Postby Shimmo7 » Mon Dec 02, 2013 4:14 pm

So basically I am making a game where I have to add the clues to the game and this is my code so far for adding them:
Code: Select all
dictionary ={ }

In the clues.txt file it looks like this

What I need help with is I need to get the terms '+','#' and'*' in the dictionary along with their definitions, so + =A ,#=M AND *=N.
I think i need to use a for loop in this? Could you help me please.
Last edited by stranac on Mon Dec 02, 2013 4:33 pm, edited 1 time in total.
Reason: Added code tags. Locked.
Posts: 1
Joined: Mon Dec 02, 2013 4:06 pm

Re: Need help with for loops

Postby stranac » Mon Dec 02, 2013 4:38 pm

I think you might be looking for something like this:
Code: Select all
with open('clues.txt', 'r') as myfile: # always better to use 'with open...'
    for line in myfile:
        value, key = line.strip()
        dictionary[key] = value

This is assuming that there are exactly two characters in a line.
If there can be differences, you need to figure out how to determine what's the key, and what's the value.

Also, please read this before posting again.
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
Posts: 1790
Joined: Thu Feb 07, 2013 3:42 pm

Return to General Discussions

Who is online

Users browsing this forum: Bing [Bot] and 6 guests