cs50 pset7包含语法

时间:2020-08-20 02:02:05

标签: python sql syntax-error cs50

我正在解决问题的roster.py部分,但出现错误:

   File "roster.py", line 17
    print(f"{row["first"]} {row["middle"]} {row["last"]}, born {row["birth"]}")
                      ^
SyntaxError: invalid syntax

我的代码:

import sys
from cs50 import SQL

if len(sys.argv) != 2:
    print("Needs two command-line argument")
    exit(1)

db = SQL("sqlite:///students.db")

house = sys.argv[1]

results = db.execute("SELECT * FROM students WHERE house = ?", house)


for row in results:
    if row["middle"] != None:
        print(f"{row["first"]} {row["middle"]} {row["last"]}, born {row["birth"]}")
    else:
        print(f"{row["first"]} {row["last"]}, born {row["birth"]}")

2 个答案:

答案 0 :(得分:2)

您的报价有误。试试这个:

for row in results:
    if row["middle"] != None:
        print(f'{row["first"]} {row["middle"]} {row["last"]}, born {row["birth"]}')
    else:
        print(f'{row["first"]} {row["last"]}, born {row["birth"]}')

答案 1 :(得分:0)

如果您尝试打印row [“ middle”]值,依此类推。

for row in results:
    if row["middle"] != None:
        print(f"{"+row["first"]+"} {"+row["middle"]+"} {"+row["last"]+"}, born {"+row["birth"]+"}")
    else:
       print(f"{"+row["first"]+"} {"+row["last"]+"}, born {"+row["birth"]+"}")