Newline (NuBe Question)

Cameron Simpson cs at cskk.id.au
Wed Nov 15 03:50:04 EST 2023


On 15Nov2023 07:25, Grizzy Adams <RealGrizzlyAdams at vivaldi.net> wrote:
>Have this (from an online "classes" tutorial)

Response inline below.

>students = []
>grades = []
>for s in geographyClass:
>	students.append(geographyStudent(s))
>for s in students:
>                grades.append(s.school)
>                grades.append(s.name)
>                grades.append(s.finalGrade())
>                if s.finalGrade()>82:
>                        grades.append("Pass")
>                else:
>                        grades.append("Fail")
>print(grades)
>
>--- End Code Snippit  ---
>
>I have extended (from tutorial) it a bit, I would really like to have a newline
>at end of each record, I have searched (and tested) but cant get "\n" 
>to give a newline, I get "Mydata\n"

It would be useful to:
- see some of the things you've tried
- what their output actually was
- what output you actually want to achieve

>Do I need to replace "append" with "print", or is there a way to get the
>newline in as I append to list?

I think you're confusing output (print) with your data (the list of 
grades).

Is the code above genuinely what you're running?

I ask because it looks to me that you're:
- appending all the individual grade fields (school, name, ...) to one 
   long grades list containing all the data from all the students
- you're printing that single enormous list in one go at the end

What I'm imagine you want is one line of grade information per student.

Remember that indentation is important in Python. You're grades code 
looks like this:

     grades = []
     for s in students:
                     grades.append(s.school)
                     grades.append(s.name)
                     grades.append(s.finalGrade())
                     if s.finalGrade()>82:
                             grades.append("Pass")
                     else:
                             grades.append("Fail")
     print(grades)

This:
- makes an empty list
- gathers up all of the student data
- prints the data in one go

I think you may want to do the first and last steps on a per student 
basis, not just once at the start and the end. So you might want to 
rearrange things:

     for s in students:
                     grades = []
                     grades.append(s.school)
                     grades.append(s.name)
                     grades.append(s.finalGrade())
                     if s.finalGrade()>82:
                             grades.append("Pass")
                     else:
                             grades.append("Fail")
                     print(grades)

Cheers,
Cameron Simpson <cs at cskk.id.au>


More information about the Python-list mailing list