0

Trying to finish roster.py and my output is incorrect. It prints the None instead of the last name and I am getting and extra space after the last printed name, such as "None , born 1981" or "Jean , born 1979". Any help would be much appreciated because this is my first time coding and my google searches have been fruitless.

import cs50
import sys
import sqlite3

db = sqlite3.connect("students.db")

cursor = db.cursor()

if (len(sys.argv) != 2):
    print("Usage: python roster.py -house")

house_name = sys.argv[1],
cursor.execute("SELECT first, middle, last, birth FROM students WHERE house = ? ORDER BY last", house_name)
row = cursor.fetchone()

for row in cursor:
    print(row[0], row[1] if (row[1] != 'None') else row[2],', born', row[3])

The output I get is below

1 Answer 1

2

Some problems detected:

  1. This 'None' is the string literal, the word "None". If import.py works according to spec, row[1] will be Null if student does not have a middle name. students.middle_name should never be "None".
  2. This row[1] if (row[1] != 'None') else row[2] is only going to print one thing, either row[1] ("middle name") or row[2] ("last name"). Output needs to print 1 or 2 "things".
  3. The extra space is because the default separator for print() is a space; ie it outputs a space wherever it "sees" a comma.
2
  • Could you give a sample code that I could use here to fix the 'None' problem? I have tried everything I can think of, even changed to Null and it will still print 'None'. Current code: ``` for row in cursor: if(row[1] != 'Null'): print(row[0], row[1], row[2],', born', row[3]) else: print(row[0], row[2],', born', row[3]) ```
    – Grendernaz
    Mar 14, 2020 at 1:32
  • Remove the ' around the word None. That makes it the string "None", as opposed to the python None object. Mar 14, 2020 at 1:40

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .