Code not returned as is supposed to

Hi, I’m following a YouTube video with some python simple programming exercises, but my returns are not what they should be. Such as:

student is asked to create a program to find the largest number in a list:

numbers = [3,6,2,4,8,10]
max=numbers[0]
for item in numbers:
    if item>max:
        max=item
        print(max)

Thus the program should return only 10, claims the author of the video, because the program compares successively, each number with the first item and keeps only the largest. But in fact the program returns 6,8,10. Which is logical to me, because the way the code is written, there are three numbers>numbers[0]. But why this difference? And how should the code be written to get the desired return?

Another example:
student is asked to draw an F using only for loops - no print(* ‘*’)


**


**
**
according to the video Solution is:

numbers=[5,2,5,2,2]
for item in numbers:
    stars = [""]
    for stuff in range(item):
        stars+="*"
        print(stars)

However, this does not return the required result, the F, but something like
*
**




**, etc. Which is again logical, because the program iterates the whole range of each item, not only the final value. But why? Is it a matter of the Python version (video is from 2020)? And how should the code look like to get the result?

maybe,

for item in numbers:
    stars = ''
    for stuff in range(item):
        stars += '*'
    print(stars)

dont see the use of a list here.
could refactor a bit using walrus operator and avoid numbers variable as it is used only once.

for item in [5, 2, 5, 2, 2]:
    stars = ''
    [stars := stars + '*' for stuff in range(item)]
    print(stars)

stuff variable is not used, could use _ as a dont care.

for item in [5, 2, 5, 2, 2]:
    stars = ''
    [stars := stars + '*' for _ in range(item)]
    print(stars)

or could even do,

[print(i*'*') for i in numbers]

for the first question,

l = [4, 3, 8, 1, 5]
max_ = l[0]
for i in l:
  if i > max_:
    max_ = i
print(max_)

but we could directly use, max(l) also

Hi,
In both your examples the print should be unindented like this.

#!python3.10
numbers=[5,2,5,2,2]
for item in numbers:
    stars = [""]
    for stuff in range(item):
        stars+="*"
    print('F', stars)

numbers = [3,6,2,4,8,10]
max=numbers[0]
for item in numbers:
    if item>max:
        max=item
print('Max', max)

You are printing something out every time you go through the F loop or find a new maximum.
John

Thanks. You provide interesting solutions, but some of them surpass my current coding knowledge - like [stars := stars + ‘*’ for stuff in range(item)] and walrus operator.

You are right, thanks. Still not sufficiently aware at this stage of my coding journey that a small syntax error can produce a big difference.

we could make one more change here, to print the character F asynchronously, could refer this article to attempt it, Python 3.11 Preview: Task and Exception Groups – Real Python

You should give us a link to the video so we can see what they really said/showed.

2 Likes

thx for the answers. i have though another question. in this piece of code:

for stuff in range(item):
        stars+="*"
print(stars)

I would have expected the stars to be displayed vertically, not horizontally, so not as an 'F". Because for the first item, 5, range(5) returns like this:
0
1
2
3
4
and not like this:
0 1 2 3 4. So when you add an “" you shoudon’t get an “F” but a long line of "” on the vertical. You still get it though. Where am i wrong?

Here is the video, case discussed here is 1:54 -1:56

In the course the use of indentation to group statements was shortly explained in the “If statement” chapter: Python Tutorial - Python Full Course for Beginners - YouTube I think the instructor should have used more time to explain it.

Much better explanation is for example here:


print implicitly adds a newline character at the end. This newline character causes further output to continue from the beginning of the next line.

You are running print once so there will be just a single newline at the very end. Consequently the output will be on just a single line.