打开和关闭文件会返回语法错误

时间:2019-04-02 01:27:17

标签: python syntax-error

我正在制作一些签入系统,以防万一我想像机器人一样构建。但是我被困住了,因为我的代码返回语法错误。有人可以帮我解决这个问题吗?

我正在在线IDE之一Repl.it上编写此代码。我无法在Eclipse上测试它,因为我的python无法正常工作。

import datetime

#This is where Name variable goes
#checkin = open(check-in.txt", "r")

i=0
while True:
  it = input("Type in input: ")
  if it == "Check-in list":
    checkin = open("check-in.txt", "r")
    if checkin.mode == "r":
        contents = checkin.read()
        print(contents)
        checkin.close()
  elif it == "Check-in":
    checkin = open("check-in.txt", "a")
    if checkin.mode == "a":
      currentDT = datetime.datetime.now()
      checkin.write((str(i+1) +". " + username + ":" + str(currentDT))
    checkin.close()

发生错误的地方

checkin.close()

我期望输出“输入中键入:”,并且当我键入“签入”时,程序应添加数字顺序,名称和时间。

输出为:“ SyntaxError:无效语法”

2 个答案:

答案 0 :(得分:0)

您的括号不平衡。在您的X = [] Y = [] with open("data.csv") as data: lines = data.read().split('\n') # headers is not being used in this spinet headers = lines[0] lines = lines[1:] # changing variable name for better reading for line in lines: X.append(line[0]) Y.append(line[1]) print(X) print(Y) 行之后添加一个额外的)

答案 1 :(得分:0)

checkin.write((str(i+1) +". " + username + ":" + str(currentDT))中缺少括号。这是固定版本:checkin.write((str(i+1) +". " + username + ":" + str(currentDT)))