5
print (
"""    Welcome to the code breaker game!
       In this game you will have to change symbols into letters in order to decipher secret words. 
       0 - instructions
       1 - start
       2 - clues
       3 - check your answers
       4 - quit
""")

choice = input(" choice : ")

if choice == ("0"):
    text_file = open ("instructions.txt","r")
    print (text_file.read())
    text_file.close()

elif choice =="1":
    text_file = open ("words.txt","r")
    contents = text_file
    print (text_file.read())
    text_file.close()
    a = input("Please enter a symbol ")
    b = input("Please enter a letter ")

    newcontents = contents.replace(a,b)
    contents = newcontents
    print(contents,"\n")
    text_file.close


elif choice == "2":
 text_file = open ("clues.txt","r")
 print (text_file.read())
 text_file.close()

elif choice == "3":
 text_file = open ("solved.txt","r")
 print (text_file.read())
 text_file.close()

elif choice == "4":
 quit 

So basically I'm doing a computer science project and my task is to make a decoding game by substituting symbols to letters but I get this error for when I try to make the part of the code which actually changes symbols into letters.

Also is there any way to make this loop (not using while loops as they are quite complicated)? I basically want the code to show me the A and B when I run the program and for me after choosing any option to be able to choose a different option. (Eg I press 0 for instructions and then be able to choose a different option such as start game).

Artjom B.
  • 61,146
  • 24
  • 125
  • 222
user3661051
  • 53
  • 1
  • 1
  • 3
  • What you actually need to do is take the text from the file, do what you want with it and then finally rewrite that data. – anon582847382 May 21 '14 at 13:52
  • The error is: Traceback (most recent call last): File "\\server2.school.local\pupils$\11.jakub\year 10\Computer science\Computer science project\Year 10 Computer science project\Code 2014\NEWPYTHONCODE.py", line 26, in newcontents = contents.replace(a,b) AttributeError: '_io.TextIOWrapper' object has no attribute 'replace' – user3661051 May 21 '14 at 13:52
  • I can't as the project specification says I have to use the provided .txts file's – user3661051 May 21 '14 at 13:54
  • Your sub-question duplicates e.g. [this one](http://stackoverflow.com/questions/18791882/python-how-to-make-program-go-back-to-the-top-of-the-code-instead-of-closing). Just use a `while` loop; they aren't that complicated. – jonrsharpe May 21 '14 at 13:59

1 Answers1

7

This part of your code:

text_file = open ("words.txt","r")
contents = text_file
print (text_file.read())
text_file.close()

makes no sense. You are assigning the file object (not the file's contents) to contents. You then print the contents, but don't assign them to anything. I think what you want is:

with open("words.txt") as text_file:
    contents = text_file.read()
print(contents)
jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
  • Ok this helped a lot as it prints the coded words now with the changes but it won't ask me for a new pairing of a symbol to letter substitute,do you know how I could do that? – user3661051 May 21 '14 at 14:01
  • @user3661051 see my comment on the question. There are numerous duplicates on SO. – jonrsharpe May 21 '14 at 14:02