【问题标题】:Why do I get an invalid syntax error in this piece of code, Python?为什么我在这段代码 Python 中得到一个无效的语法错误?
【发布时间】:2015-08-04 03:08:12
【问题描述】:

代码:

students = []
choice = None
while choice != 0:

      print(
    """
    0 - Exit
    1 - Show all students
    2 - Add a student
    """
    )
      choice = input("Choice: ")
      print()
      if choice == "0":
          print("Goodbye")
          break
      elif choice == "1":
           print("\nStudents: ")
           for entry in students:
                 email, name, number = entry
                 print(name, "\t", email, "\t", number)
      elif choice == "2":
          name = input("What is the students name?")
          email = input("What is the students email adress? ")
          number = int(input("What is the students number? ")      
          entry = email, name, number
          students.append(info)
          students.sort(reverse=False)
          student = students
      else:
          print("Sorry, but", choice, "isn't a valid choice.")

当我在编译器中运行它时,我得到了该行的语法错误

entry = email, name, number

我不知道为什么,请告诉我。

【问题讨论】:

  • 请记住,报告的行是检测到错误的行,不一定是包含错误的行。这是因为换行符在 ( ) 中是有效的,所以如果你缺少 ),编译器直到(至少)下一行才会知道这是一个错误。

标签: python list append tuples


【解决方案1】:

您在错误所在行的正上方缺少)

number = int(input("What is the students number? ")  #here 
entry = email, name, number

通常,缺少括号会导致堆栈跟踪指向紧随其后的行。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-08
    • 2013-01-06
    • 2021-03-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多