1

我编写了这个示例程序,旨在从计算机打开一个文本文件(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()
4

1 回答 1

3

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"
于 2013-06-21T18:30:19.457 回答