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 ={ }
myfile=open('clues.txt','r')
textclues=myfile.read()


In the clues.txt file it looks like this
A+
M#
N*

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.
Shimmo7
 
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
stranac
 
Posts: 1218
Joined: Thu Feb 07, 2013 3:42 pm


Return to General Discussions

Who is online

Users browsing this forum: W3C [Linkcheck] and 2 guests