Wednesday, November 15, 2023 at 9:50, Alan Gauld via Python-list wrote: Re: Newline (NuBe Question) (at least in part)
>On 15/11/2023 07:25, Grizzy Adams via Python-list wrote: >> 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 --- >> Do I need to replace "append" with "print", or is there a way to get the >> newline in as I append to list? >Firstly, it is usually a bad idea to mix formatting features(like >newline) with the data. You will need to remove them again if you want >to work with the data itself. True, I onlt went that way when my (vain) attempts to add a newline any other way, my usual language is VB/VBA, Delphi or C++ at a push, so I'm really a nube here >So, better to print the raw data and add the formatting during printing. >There are a couple of options here (well more than a couple actually!) >The simplest is to create another for loop and print each field with a >newline automatically added by print() >Another is to simply join everything in grades together separated by >newlines. Python has a method to do that called join(): >print('\n'.join(grades)) >Unfortunately it seems your data has a mix of strings and numbers so >that won't work without some tweaks: >print('\n'.join(str(f) for f in grades)) that gets closer (sort of) my old code gave on long (only 5 records so far) list, ['Example High', 'Mary', 89.6, 'Pass', 'Example High', 'Matthew', 76.5, 'Fail', 'Example High', 'Marie', 80.4, 'Fail', 'Example High', 'Manuel', 79.6, 'Fail', 'Example High', 'Malala', 98.9, 'Pass'] your code gives one tall column, Example High Mary 89.6 Pass Example High Matthew 76.5 Fail Example High Marie 80.4 Fail Example High Manuel 79.6 Fail Example High Malala 98.9 Pass my ideal one row for each student (this I have edited manually) Example High, Mary, 89.6, Pass Example High, Matthew, 76.5, Fail Example High, Marie, 80.4, Fail Example High, Manuel, 79.6, Fail Example High, Malala, 98.9, Pass >However, I wonder if this really what you want? You have created grades as a >long list containing all of the attributes of all of the students plus their >Pass/Fail status. But you have no (easy)way to access the Pass/Fail value for >each student. Do you really want to store the Pass/Fail in the student? And >then print the students? Like so: I do want to keep the data in tact, incase it gets reused later in the tutorial(s) >Just a thought... >PS. There are neater ways to do this but you may not have covered >those yet so I'll stick to basics. I already jumped forward a bit (to python Classes) Thanks -- https://mail.python.org/mailman/listinfo/python-list