Python(最新版本)语法错误



我编写了这个示例程序,用于从计算机打开一个文本文件(database1.txt),并显示文件中当前的结果。然后提示使用,如果在文档中有自己的名字,则应打印文本文件的内容然后关闭,否则则应提示用户输入自己的名字,然后程序将该名称写入相同的文本文档,然后再次打印文本文件的内容,以便用户可以看到新添加的数据。我已经输入了代码,但不知怎的,它一直说我有一个语法错误。我检查了几次,我无法修复错误。我想知道是否有人可以看一下,如果他们可能能够解释错误给我。谢谢你

    #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是py3.x中的函数:

print newReadData

应为:

print (newReadData)
演示:

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

语句如下:

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

可以简化为:

elif userInput in "yes"

最新更新