Newline (NuBe Question)

Thomas Passin list1 at tompassin.net
Wed Nov 15 09:45:03 EST 2023


On 11/15/2023 2:25 AM, Grizzy Adams via Python-list wrote:
> Hi & thanks for patience with what could be simple to you
> 
> Have this (from an online "classes" tutorial)
> 
> --- Start Code Snippit  ---
> 
> 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"
> 
> Do I need to replace "append" with "print", or is there a way to get the
> newline in as I append to list?

First of all, if this is an accurate representation of the course 
material, you need a better course.  There's no sense in appending all 
those values one after another in a single list since later it will be 
very inconvenient to detect the end of one student's info and the start 
of the next one's.  And if you don't need to know that, but just want to 
print out the data, you don't need to a list at all, just print it out 
in the loop.

A list that contains lists of each student's data, one per interior 
list, would make more sense.

Second, it is usual to append data to a list without print formatting, 
and then add your formatting when you go to print the list.  That way 
you can use the list for other things beyond just printing, and the code 
is clearer and simpler as well.

You may see responses that suggest various code alternatives.  But you 
haven't shown us an example of what you want the output to look like, 
and you haven't said what else you plan to use the list for.  So anyone 
who responds has to fly blind, without knowing key information.

Asking for help is like writing code, with an added social element.  You 
have to be clear about the requirements, inputs, and desired outputs, 
and you have to organize your request in a way that's easy for others to 
understand and be willing to help.  Your original post is partway there 
already.



More information about the Python-list mailing list