1

I wrote this sample program that is meant to open a text file (database1.txt) from the computer, and display the results that are currently in the file. Then prompt the use if their name is in the document, if it is it should print the contents of the text file then close, otherwise it should prompt the user to enter their first name, then the program writes the name into the same text document, then prints the contents of the text file again so that the user can see the new added data. I have typed the code, and somehow it keeps saying I have a syntax error. I checked a few times and I cannot fix the error. I was wondering if someone could take a look and if they might be able to explain the error to me. Thank you

 #This program reads/writes information from/to the database1.txt file
def database_1_reader ():
print('Opening database1.txt')
f = open('database1.txt', 'r+')
data = f.read()
print data
print('Is your name in this document? ')
userInput = input('For Yes type yes or y. For No type no or n ').lower()
if userInput == "no" or userInput == "n"
 newData = input('Please type only your First Name. ')
 f.write(newData)
 f = open ('database1.txt', 'r+')
 newReadData = f.read()
 print newReadData
 f.close()
elif userInput == "yes" or userInput == "ye" or userInput == "y"
 print data
 f.close()
else:
 print("You b00n!, You did not make a valid selection, try again ")
 f.close()
input("Presss any key to exit the program")
database_1_reader()
Jon Clements
143k34 gold badges254 silver badges288 bronze badges
asked Jun 21, 2013 at 18:29
1
  • Use with for handling files, it automatically closes the file for you. Commented Jun 21, 2013 at 18:37

1 Answer 1

3

print is a function in py3.x:

print newReadData

should be :

print (newReadData)

Demo:

>>> print "foo"
 File "<ipython-input-1-45585431d0ef>", line 1
 print "foo"
 ^
SyntaxError: invalid syntax
>>> print ("foo")
foo

statements like this:

elif userInput == "yes" or userInput == "ye" or userInput == "y"

can be reduced to :

elif userInput in "yes"
answered Jun 21, 2013 at 18:30
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.