简体   繁体   中英

Python (latest version) Syntax Error

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()

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"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM