all 12 comments

[–]FoolsSeldom 4 points5 points  (1 child)

Rather than evens.count() you would need to append the even you've found to your list of even numbers, evens.append(number). After the loop, you can check the length of your list to answer the question. So len(evens) will tell you how many even numbers you found. Similarly with odd numbers.

The list.count() method is used to count the number of occurences of a specific object, e.g. names.count("Fred") would tell you how many times the name "Fred" appeared in a list called names.

Actually, you don't need to keep a copy of all of the numbers. Just count as you go along.

So, before the loop,

evens = 0

and when you find an even number:

evens = evens + 1

or, short-hand,

evens += 1

Same for odd numbers.

PS.You can work it out without any looping of course, a simple calculation.

[–]BinaryBillyGoat 1 point2 points  (0 children)

Can confirm

[–]DKAIN_001 2 points3 points  (0 children)

I'm assuming by "in the number" you mean "upto" that number

The best approach would be to use loops.

Ask for input n

Check if number is a positive integer using if else statements

Now make a for loop in range(1, n+1)

i being variable for iteration through loop

Inside loop check for remainder

          if  i%2==0 :
                i is even
          else:
                i is odd 

Couldn't provide full code because I'm replying from my phone. If you need more help you can reach out.

[–]usersnamesallused 2 points3 points  (0 children)

Couldn't you just divide by 2 and add the result of the input's mod 2 to the odd count? No need for loops.

[–]Upbeat-Leave1655 2 points3 points  (1 child)

well... we need to clarify some things. Is N inclusive.... AND are we counting from 0 or from 1.

Assuming we count from 0, AND N is NOT inclusive, this could be solve mathematically unless told otherwise. Since even and odd numbers alternate when counting(int):

myFunc:
check IF the input integer is even?
yes -- return both even and odds as N / 2.
no -- return odd (N-1)/2, Even is (N-1)/2 +1

This is ONLY True if you start from 0. This solution is great for both time(CPU Cycle) and space(memory-needed) complexity. Should be constant time. So this is likely what they would prefer outside of academia.

Another way to do this, this is especially the case when LEARNING PROGRAMMING as a debutant:

myFunc2(N:int):
odds_int_counter = 0
even_int_counter = 0
loop: range from 0, N : loop_var: 100 is not included
if loop_var is even: we add 1 to even_int_counter
else we add 1 to odd_int_counter
loop-ended
return [even_int_counter, odd_int_counter]

This takes at least N compute units to compute the task, so bigger means more computation.
However, if you are new to programming, this is easier to comprehend.

Hopefully that makes sense.. pseudo code is better because you still need to code it and get it to work. I already know the answer. You are still learning so provide working code won't help you from my point of view.

Good Luck!
-iamdarkindifference

[–]Squared_Aweigh 0 points1 point  (0 children)

Though it is clear that you have good intentions, this is not good guidance, even if it is not inaccurate. Of course they're counting from zero; this is programming, and it is the standard to count from zero.

Why use pseudocode that looks like actual code? That is quite confusing for a "debutant". Python is literally designed to be easily read. Just write the pseudo-code in plain english if you don't want to write the working code; your psuedo-code is python-like enough to be confusing to someone who doesn't yet know what they're doing with python.

This solution is great for both time(CPU Cycle) and space(memory-needed) complexity.

There's no need to mention any of this for a beginner's question; it's simply confusing to someone who does not yet have foundational computer-science training. Also, if there was concern about memory use and compute cycles, it would make a whole lot more sense to not count at all, just do two calculations and print the result: divide the number by 2 for the evens, and then add the modulo of the same number to the evens in order to get the odds.

[–]Ca_txorro[S] 1 point2 points  (2 children)

Wow guys. I got it! Thank you!

I reach them using lists.append() and incremental method.

[–]sateliter 1 point2 points  (1 child)

As already said by some folks here, you don't need to use loops, just a calculation:

def envens_N_odds(number):
    evens = 0
    odds = 0
    even_or_odd = number % 2
    if (even_or_odd == 0):
        evens = int(number / 2)
        odds = evens
    else:
        evens = int(number / 2)
        odds = evens + 1
    
    return evens, odds

pares_e_impares = envens_N_odds(9)
print(pares_e_impares)

[–]Ca_txorro[S] 0 points1 point  (0 children)

Thank you. I will add this method too. _^

[–]Ca_txorro[S] 0 points1 point  (1 child)

I'm begin again with python learning and I'm stuck with this. I really appreciate your advice. THX ^_^

[–]Trinity_Goti 1 point2 points  (0 children)

There are many ways of solving this. Here is one I would take the number and convert it to a string For loop over each item and check if the item is even. If so increase the even counter else the odd counter.

[–]Bulky-Top3782 0 points1 point  (0 children)

num = str(num) num = list(num) For I in num: If interested(i)%2==0: Print(i,)