我编写了这个示例程序,用于从计算机打开文本文件(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()
答案 0 :(得分: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"