Python Forum
I am a newbie.Help me with this simple piece of code
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
I am a newbie.Help me with this simple piece of code
#1
I was watching this complete Python Tutorial by Mosh Hamadani on youtube.
I saw a small piece of code that he wrote and I wrote it in pycharm.
I got an output that was different from what he got.
This is a program to find the largest number.

This is the piece of code.

numbers = [11, 2, 23, 45, 67, 99, 101]
largest_num = numbers[0]
for number in numbers:
    if number > largest_num:
        largest_num = number
        print(largest_num)
The result was
Output:
23 45 67 99 101
But, The intended result was 101, the largest number.
Where did I go wrong?
Reply
#2
you want line#6 out of the loop body (i.e. unindent it 2 levels).
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
(Jan-08-2020, 09:31 AM)buran Wrote: you want line#6 out of the loop body (i.e. unindent it 2 levels).

Thank you.
Reply
#4
Maybe following can enhance learning process.

In programming same results can be achieved in different ways.

For example we don't need to compare all list items if we have assigned first value as max value. We can start from second item:

numbers = [11, 2, 23, 45, 67, 99, 101] 
largest_num = numbers[0]
for number in numbers[1:]: 
    if largest_num < number: 
        largest_num = number

# largest_num is 101
We can make an iterator from numbers and assign first item in list with next() as largest_num and then iterate over remaining items:

numbers = [11, 2, 23, 45, 67, 99, 101]
nums = iter(numbers)
largest_num = next(nums)
for num in nums: 
    if largest_num < num: 
        largest_num = num 
# largest_num is 101
After mastering finding largest number from list with own algorithm one can start using built-in max() (no need to invent wheel):

>>> numbers = [11, 2, 23, 45, 67, 99, 101]
>>> max(numbers)
101
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Newbie - code solution explained Stjude1982 2 129 Yesterday, 08:54 AM
Last Post: Stjude1982
  My simple code don't works !! Nabi666 1 182 Sep-06-2021, 12:10 PM
Last Post: jefsummers
Sad SyntaxError: from simple python example file from mind-monitor code (muse 2) warmcupoftea 4 345 Jul-16-2021, 02:51 PM
Last Post: warmcupoftea
  Plotting sum of data files using simple code Laplace12 3 673 Jun-16-2021, 02:06 PM
Last Post: BashBedlam
  .maketrans() - a piece of code which needs some explanation InputOutput007 5 637 Jan-28-2021, 05:05 PM
Last Post: buran
  newbie need help update code Mariaa 3 775 Oct-02-2020, 08:12 AM
Last Post: ibreeden
  Help with isinstance command (very simple code) Laplace12 2 685 Jul-30-2020, 05:26 AM
Last Post: Laplace12
  Simple code help bntayfur 2 651 Jul-05-2020, 07:47 PM
Last Post: menator01
  Simple code error please help bntayfur 7 1,242 Jun-08-2020, 02:22 AM
Last Post: pyzyx3qwerty
  Simple python code error cls0724 5 1,344 Mar-12-2020, 07:45 PM
Last Post: stullis

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020