Re: Newline (NuBe Question)

2023-11-15 Thread Grizzy Adams via Python-list
Wednesday, November 15, 2023  at 15:54, Thomas Passin via Python-list wrote:
Re: Newline (NuBe Question) (at least in part)

>On 11/15/2023 2:04 PM, Grizzy Adams via Python-list wrote:
>> Wednesday, November 15, 2023  at 12:19, Pierre Fortin wrote:
>> Re: Newline (NuBe Question) (at least in part)
 
>>> On Wed, 15 Nov 2023 16:51:09 - Grizzy Adams via Python-list wrote:

>>> I don't give solutions; just a nudge...  you appear not to fully grok
>>> "list"; your list is ONE list with no delineation between students. You
>>> want a "list of lists"...
 
 ['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']

>>> Like this:
 
>>> students = [
>>> ['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']
>>> ]
 
>> for now I made a copt of code and altered to
>> 
>> students = []
>> grades = []

># In this design there is no point in the extra loop.
># also, the indentation is wrong.  Perhaps you inserted
># tabs?  Use only spaces in these posts.

I copy-pasted the code direct from IDLE, (to avoid any other typo's) 

># Also you don't need the students list
>> for student in geographyClass:
>>  # students.append(geographyStudent(s))
> s = geographyStudent(student)
>> 
>  # for s in students:
>>   if s.finalGrade()>82: Result=("Pass")
>>   else: Result=("Fail")
>>   print(s.school, s.name, s.finalGrade(),Result)

I'll hive this a try (as a learning point)

Thanks
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Newline (NuBe Question)

2023-11-15 Thread Thomas Passin via Python-list

On 11/15/2023 2:04 PM, Grizzy Adams via Python-list wrote:

Wednesday, November 15, 2023  at 12:19, Pierre Fortin wrote:
Re: Newline (NuBe Question) (at least in part)


On Wed, 15 Nov 2023 16:51:09 - Grizzy Adams via Python-list wrote:

I don't give solutions; just a nudge...  you appear not to fully grok
"list"; your list is ONE list with no delineation between students. You
want a "list of lists"...



['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']



Like this:



students = [
['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']
]


for now I made a copt of code and altered to

students = []
grades = []


# In this design there is no point in the extra loop.
# also, the indentation is wrong.  Perhaps you inserted
# tabs?  Use only spaces in these posts.
# Also you don't need the students list

for student in geographyClass:
# students.append(geographyStudent(s))

s = geographyStudent(student)



 # for s in students:

  if s.finalGrade()>82: Result=("Pass")
  else: Result=("Fail")
  print(s.school, s.name, s.finalGrade(),Result)


This may help get you headed in the right direction:



for s in students:
print( s )



Hint: look forward to learning about f-strings...


I will look forward to them, may even go search ahead,


--
https://mail.python.org/mailman/listinfo/python-list


Re: Newline (NuBe Question)

2023-11-15 Thread Grizzy Adams via Python-list
Wednesday, November 15, 2023  at 12:19, Pierre Fortin wrote:
Re: Newline (NuBe Question) (at least in part)

>On Wed, 15 Nov 2023 16:51:09 - Grizzy Adams via Python-list wrote:
>
>I don't give solutions; just a nudge...  you appear not to fully grok
>"list"; your list is ONE list with no delineation between students. You
>want a "list of lists"...

>>['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']

>Like this:

>students = [
>['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']
>]

for now I made a copt of code and altered to 

students = []
grades = []
for s in geographyClass:
students.append(geographyStudent(s))

for s in students:
 if s.finalGrade()>82: Result=("Pass")
   else: Result=("Fail")
   print(s.school, s.name, s.finalGrade(),Result)

>This may help get you headed in the right direction:

>for s in students:
>print( s )

>Hint: look forward to learning about f-strings...

I will look forward to them, may even go search ahead, 
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: xor operator

2023-11-15 Thread Dom Grigonis via Python-list
In case someone is actually going to execute the code, there is a bug:

`set` need to be wrapped in `len` for criteria args.

> On 15 Nov 2023, at 20:13, Dom Grigonis  wrote:
> 
> 
> The specific situation was related to truth values and following out of that 
> my considerations regarding equivalent of all and any for counting `truths`.
> 
> So one of the specific examples:
> class Publisher:
> def __init__(self):
> self.subscribers = dict()
> 
> def subscribe(self, sub, criteria, agg=all):
> self.subscribers[sub] = (criteria, agg)
> 
> def publish(self, msg):
> for sub, criteria in self.subscribers.items():
> if criteria(msg):
> sub.handle(msg)
> 
> p = Publisher()
> p.subscribe(sub, lambda x: all(r > 3 for r in x.ratings))
> 
> # So what I needed is:
> p.subscribe(sub, lambda x: set(r > 3 for r in x.ratings) > 50)
> # Note, that elements might not necessarily be bool.
> # E.g. at least 51 non-empty pages
> p.subscribe(sub, lambda x: set(bool(p) for p in x.pages) > 50)
> # So the function:
> p.subscribe(sub, lambda x: xor_more_than(x.pages, 50)
> # would ideally deal with truth values too.
> The question is: can you construct a function? which:
> a) performs: lambda x: set(bool(el) for el in iterable) > n
> b) is in line with performance of python’s `all`
> 
> Then, as I said the case where one would need `set() == n` is hard to think 
> of, but it seems fairly probable to need `set() > n` and `set() < n`.
> 
> Regards,
> DG
> 
>> On 15 Nov 2023, at 19:16, Peter J. Holzer via Python-list 
>> mailto:python-list@python.org>> wrote:
>> 
>> On 2023-11-15 12:26:32 +0200, Dom Grigonis wrote:
>>> 
>>> Thank you,
>>> 
>>> 
>>> test2 = [True] * 100 + [False] * 2
>>> test2i = list(range(100))
>>> 
>>> %timeit len(set(test2i)) == 1   # 1.6 µs ± 63.6 ns per loop (mean ± std. 
>>> dev. of 7 runs, 1,000,000 loops each)
>>> %timeit all(test2)  # 386 ns ± 9.58 ns per loop (mean ± std. 
>>> dev. of 7 runs, 1,000,000 loops each)
>>> 
>>> test2s = set(test2i)
>>> %timeit len(test2s) == 1# 46.1 ns ± 1.65 ns per loop (mean ± std. 
>>> dev. of 7 runs, 10,000,000 loops each)
>>> 
>>> If you pre-convert to set it is obviously faster. However, set
>>> operation is most likely going to be part of the procedure. In which
>>> case it ends up to be significantly slower.
>> 
>> Obviously, if you convert a list to a set just to count the elements
>> it's going to be slow. My suggestion was to use the set *instead* of the
>> list. I don't know whether that's possible in your situation, because
>> you haven't told us anything about it. All I'm suggesting is taking a
>> step back and reconsider your choice of data structure.
>> 
>>hp
>> -- 
>>   _  | Peter J. Holzer| Story must make more sense than reality.
>> |_|_) ||
>> | |   | h...@hjp.at  |-- Charles Stross, 
>> "Creative writing
>> __/   | http://www.hjp.at/  |   challenge!"
>> -- 
>> https://mail.python.org/mailman/listinfo/python-list 
>> 
> 

-- 
https://mail.python.org/mailman/listinfo/python-list


__set_name__ equivalent for instance

2023-11-15 Thread Dom Grigonis via Python-list
So there is a method __set_name__ which is called on class creation.

The functionality that I am interested in is not retrieving name, but the fact 
that it also receives `owner` argument.

Thus, allowing simulation of bound class method.

I was wandering if there is an equivalent functionality of attribute to receive 
`instance` argument on instance creation.

Regards,
DG
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: xor operator

2023-11-15 Thread Dom Grigonis via Python-list

The specific situation was related to truth values and following out of that my 
considerations regarding equivalent of all and any for counting `truths`.

So one of the specific examples:
class Publisher:
def __init__(self):
self.subscribers = dict()

def subscribe(self, sub, criteria, agg=all):
self.subscribers[sub] = (criteria, agg)

def publish(self, msg):
for sub, criteria in self.subscribers.items():
if criteria(msg):
sub.handle(msg)

p = Publisher()
p.subscribe(sub, lambda x: all(r > 3 for r in x.ratings))

# So what I needed is:
p.subscribe(sub, lambda x: set(r > 3 for r in x.ratings) > 50)
# Note, that elements might not necessarily be bool.
# E.g. at least 51 non-empty pages
p.subscribe(sub, lambda x: set(bool(p) for p in x.pages) > 50)
# So the function:
p.subscribe(sub, lambda x: xor_more_than(x.pages, 50)
# would ideally deal with truth values too.
The question is: can you construct a function? which:
a) performs: lambda x: set(bool(el) for el in iterable) > n
b) is in line with performance of python’s `all`

Then, as I said the case where one would need `set() == n` is hard to think of, 
but it seems fairly probable to need `set() > n` and `set() < n`.

Regards,
DG

> On 15 Nov 2023, at 19:16, Peter J. Holzer via Python-list 
>  wrote:
> 
> On 2023-11-15 12:26:32 +0200, Dom Grigonis wrote:
>> 
>> Thank you,
>> 
>> 
>> test2 = [True] * 100 + [False] * 2
>> test2i = list(range(100))
>> 
>> %timeit len(set(test2i)) == 1   # 1.6 µs ± 63.6 ns per loop (mean ± std. 
>> dev. of 7 runs, 1,000,000 loops each)
>> %timeit all(test2)  # 386 ns ± 9.58 ns per loop (mean ± std. 
>> dev. of 7 runs, 1,000,000 loops each)
>> 
>> test2s = set(test2i)
>> %timeit len(test2s) == 1# 46.1 ns ± 1.65 ns per loop (mean ± std. 
>> dev. of 7 runs, 10,000,000 loops each)
>> 
>> If you pre-convert to set it is obviously faster. However, set
>> operation is most likely going to be part of the procedure. In which
>> case it ends up to be significantly slower.
> 
> Obviously, if you convert a list to a set just to count the elements
> it's going to be slow. My suggestion was to use the set *instead* of the
> list. I don't know whether that's possible in your situation, because
> you haven't told us anything about it. All I'm suggesting is taking a
> step back and reconsider your choice of data structure.
> 
>hp
> -- 
>   _  | Peter J. Holzer| Story must make more sense than reality.
> |_|_) ||
> | |   | h...@hjp.at |-- Charles Stross, "Creative writing
> __/   | http://www.hjp.at/ |   challenge!"
> -- 
> https://mail.python.org/mailman/listinfo/python-list

-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Newline (NuBe Question)

2023-11-15 Thread Pierre Fortin via Python-list
On Wed, 15 Nov 2023 16:51:09 - Grizzy Adams via Python-list wrote:

I don't give solutions; just a nudge...  you appear not to fully grok
"list"; your list is ONE list with no delineation between students. You
want a "list of lists"...

>['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']

Like this:

students = [
['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']
]

This may help get you headed in the right direction:

for s in students:
print( s )

Hint: look forward to learning about f-strings...

HTH,
Pierre
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: xor operator

2023-11-15 Thread Peter J. Holzer via Python-list
On 2023-11-15 12:26:32 +0200, Dom Grigonis wrote:
> 
> Thank you,
> 
> 
> test2 = [True] * 100 + [False] * 2
> test2i = list(range(100))
> 
> %timeit len(set(test2i)) == 1   # 1.6 µs ± 63.6 ns per loop (mean ± std. dev. 
> of 7 runs, 1,000,000 loops each)
> %timeit all(test2)  # 386 ns ± 9.58 ns per loop (mean ± std. dev. 
> of 7 runs, 1,000,000 loops each)
> 
> test2s = set(test2i)
> %timeit len(test2s) == 1# 46.1 ns ± 1.65 ns per loop (mean ± std. 
> dev. of 7 runs, 10,000,000 loops each)
> 
> If you pre-convert to set it is obviously faster. However, set
> operation is most likely going to be part of the procedure. In which
> case it ends up to be significantly slower.

Obviously, if you convert a list to a set just to count the elements
it's going to be slow. My suggestion was to use the set *instead* of the
list. I don't know whether that's possible in your situation, because
you haven't told us anything about it. All I'm suggesting is taking a
step back and reconsider your choice of data structure.

hp
-- 
   _  | Peter J. Holzer| Story must make more sense than reality.
|_|_) ||
| |   | h...@hjp.at |-- Charles Stross, "Creative writing
__/   | http://www.hjp.at/ |   challenge!"


signature.asc
Description: PGP signature
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Newline (NuBe Question)

2023-11-15 Thread Grizzy Adams via Python-list
Wednesday, November 15, 2023  at 9:45, Thomas Passin via Python-list wrote:
Re: Newline (NuBe Question) (at least in part)

>On 11/15/2023 2:25 AM, Grizzy Adams via Python-list wrote:
>> Hi & thanks for patience with what could be simple to you

>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,

Offered code got 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']

offered code gave 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 is one row for each student (I had edited manually to show this)

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
 
>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.

I'll keep list for a while in case it gets used or reused later, for now it's 
just a test bed along with a few others, I can work thru as I learn 

-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Newline (NuBe Question)

2023-11-15 Thread Thomas Passin via Python-list

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.


--
https://mail.python.org/mailman/listinfo/python-list


Re: Newline (NuBe Question)

2023-11-15 Thread Grizzy Adams via Python-list
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


Re: xor operator

2023-11-15 Thread Dom Grigonis via Python-list
Thank you,

test2 = [True] * 100 + [False] * 2
test2i = list(range(100))

%timeit len(set(test2i)) == 1   # 1.6 µs ± 63.6 ns per loop (mean ± std. dev. 
of 7 runs, 1,000,000 loops each)
%timeit all(test2)  # 386 ns ± 9.58 ns per loop (mean ± std. dev. 
of 7 runs, 1,000,000 loops each)

test2s = set(test2i)
%timeit len(test2s) == 1# 46.1 ns ± 1.65 ns per loop (mean ± std. dev. 
of 7 runs, 10,000,000 loops each)
If you pre-convert to set it is obviously faster. However, set operation is 
most likely going to be part of the procedure. In which case it ends up to be 
significantly slower.

Regards,
DG


> On 15 Nov 2023, at 02:34, Peter J. Holzer via Python-list 
>  wrote:
> 
> On 2023-11-14 00:11:30 +0200, Dom Grigonis via Python-list wrote:
>> Benchmarks:
>> test1 = [False] * 100 + [True] * 2
>> test2 = [True] * 100 + [False] * 2
>> 
>> TIMER.repeat([
>>lambda: xor(test1), # 0.0168
>>lambda: xor(test2), # 0.0172
>>lambda: xor_ss(test1),  # 0.1392
>>lambda: xor_ss(test2),  # 0.0084
>>lambda: xor_new(test1), # 0.0116
>>lambda: xor_new(test2), # 0.0074
>>lambda: all(test1), # 0.0016
>>lambda: all(test2)  # 0.0046
>> ])
>> Your first function is fairly slow.
>> Second one deals with short-circuiting, but is super slow on full search.
>> 
>> `xor_new` is the best what I could achieve using python builtins.
>> 
>> But builtin `all` has the best performance.
> 
> One question worth asking is if a list of bool is the best data
> structure for the job. This is essentially a bitmap, and a bitmap is
> equivalent to a set of integers. len(s) == 1 is also a fairly quick
> operation if s is small. On my system, len(test1s) == 1 (where test1s is
> {100, 101}) is about as fast as all(test1) and len(test2s) == 1 (where
> test2s is set(range(100))) is about twice as fast as all(test2).
> 
> If you are willing to stray from the standard library, you could e.g.
> use pyroaring instead of sets: This is about as fast as all(test1)
> whether there are two bits set or a hundred.
> 
>hp
> 
> -- 
>   _  | Peter J. Holzer| Story must make more sense than reality.
> |_|_) ||
> | |   | h...@hjp.at  |-- Charles Stross, 
> "Creative writing
> __/   | http://www.hjp.at/  |   challenge!"
> -- 
> https://mail.python.org/mailman/listinfo/python-list 
> 
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Newline (NuBe Question)

2023-11-15 Thread Cameron Simpson via Python-list

On 15Nov2023 07:25, Grizzy Adams  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 
--
https://mail.python.org/mailman/listinfo/python-list


Re: Newline (NuBe Question)

2023-11-15 Thread Alan Gauld via Python-list
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.

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))


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:

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

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.


-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Newline (NuBe Question)

2023-11-15 Thread dn via Python-list

On 15/11/2023 20.25, Grizzy Adams via Python-list wrote:

Hi & thanks for patience with what could be simple to you

Have this (from an online "classes" tutorial)


There are lots of on-line classes!



--- 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?


Don't know how "Mydata..." results - where is it in the code.

What do you see when grades is printed?
Do you really want data-values all mashed together?

Yes, what changed after removal of all the .append()-s, and instead, 
within the (second) for-loop print( school, name, ... ) was used?


Is it easier to go on from there?

--
Regards,
=dn
--
https://mail.python.org/mailman/listinfo/python-list