Python Forum
Two exact "Fors" giving me different results?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Two exact "Fors" giving me different results?
#1
So, I was practicing the For loop that I've found on the internet but if I copy-paste the original code, the list of numbers appears before the final odd an even numbers print. If I do it by myself, the numbers appear one by one before the odd and even prints...I don't know what I'm doing differently from the guy. I examined the code soo many times and I don't see any difference. My code is the one translated to Portuguese:

num = [0,1,2,3,4,5,6,7,8,9]

impar = 0
par = 0

for x in num:
    print(x)
    resto = x % 2
    if resto == 0:
        par = par + 1
    else:
        impar = impar + 1

    print("O Número de Pares é", par)
    print("O Número de Impares é", impar)
Result:

0
O Número de Pares é 1
O Número de Impares é 0
1
O Número de Pares é 1
O Número de Impares é 1
2
O Número de Pares é 2
O Número de Impares é 1
3
O Número de Pares é 2
O Número de Impares é 2
...

This is the one that I copied and it gives me the list of numbers before the last prints:

numbers = [1,2,3,4,5,6,7,8,9]

odd_number = 0
even_number = 0

for x in numbers:
    print(x)
    remainder = x % 2
    if remainder == 0:
        even_number = even_number + 1
    else:
        odd_number = odd_number + 1

print('number of even: ', even_number)
print('number of odd: ', odd_number)
Result:
1
2
3
4
5
6
7
8
9
number of even: 4
number of odd: 5

I'm new to Python and I really don't know what is the difference...I feel that there is something really obvious that I cannot see Wall Cry
Reply
#2
the indentation of lines 13 and 14. In the first snippet they are inside the loop, so they get printed with every iteration. In the second snippet they are printed once - after the loop execution has completed

Indentation matters in python
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
(May-04-2020, 12:27 PM)buran Wrote: the indentation of lines 13 and 14. In the first snippet they are inside the loop, so they get printed with every iteration. In the second snippet they are printed once - after the loop execution has completed

Indentation matters in python

OMG Shocked
I knew it was something obvious...dam Doh .
I knew about the indentation but don't know how I've forgotten to inspect that
Well, thanks for the fast response...Feel kinda dumb right now.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Question in this code, I input Key_word, it can not find although all data was exact Help me! duchien04x4 3 972 Aug-31-2023, 05:36 PM
Last Post: deanhystad
  Matching Exact String(s) Extra 4 1,859 Jan-12-2022, 04:06 PM
Last Post: Extra
  replace exact word marfer 1 6,013 Oct-11-2021, 07:08 PM
Last Post: snippsat
  Assistance with running a few lines of code at an EXACT time nethatar 5 3,166 Feb-24-2021, 10:43 PM
Last Post: nilamo
  Search Results Web results Printing the number of days in a given month and year afefDXCTN 1 2,191 Aug-21-2020, 12:20 PM
Last Post: DeaD_EyE
  Exact Match Kristenl2784 2 2,750 Jul-26-2020, 03:29 PM
Last Post: Kristenl2784
  Redirection giving faulty results monstrup 5 2,389 Jul-19-2020, 07:11 AM
Last Post: Gribouillis
  How to append one function1 results to function2 results SriRajesh 5 3,073 Jan-02-2020, 12:11 PM
Last Post: Killertjuh
  Confusion in exact term used for ? ift38375 2 2,525 Jul-22-2019, 08:58 AM
Last Post: buran
  Finding exact phrase in list graham23s 2 2,835 Mar-13-2019, 06:47 PM
Last Post: graham23s

Forum Jump:

User Panel Messages

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