Printing "count" of even numbers while-loop with if
Question:
I am suppose to be able to complete the following in Python…
Fill in the blanks to complete the function “even_numbers(n)”. This function should count how many even numbers exist in a sequence from 0 to the given “n” number, where 0 counts as an even number. For example, even_numbers(25) should return 13, and even_numbers(6) should return 4.
def even_numbers(n):
count = 0
current_number = 0
while ___ # Complete the while loop condition
if current_number % 2 == 0::
___ # Increment the appropriate variable
___ # Increment the appropriate variable
return count
print(even_numbers(25)) # Should print 13
print(even_numbers(144)) # Should print 73
print(even_numbers(1000)) # Should print 501
print(even_numbers(0)) # Should print 1
I’ve tried every variation I can think of for changing every variable I can think of. I have referenced the coursework (I’ve been stuck on this question for a solid week and counting), I have searched Stack Overflow and found multiple variations to reference not exactly what I need to do (and most well advanced from where I am learning right now… even though they solve the problem they don’t…I tried them anyway hoping a light bulb would go off but it hasn’t).
I’ve been trying to get the print(even_numbers(#)) sections to match up… make the script work.
Is there anyone who can explain this to me without giving away the answer outright? I really want to understand where I am going wrong and why. I am a complete novice and mostly self taught from books.
Attempts (a few of them)
def even_numbers(n):
count = 0
current_numbers = 0
while n > current_numbers: # Complete the while loop condition
if current_numbers % 2 == 0:
count = count + 1 # Increment the appropriate variable
else:
current_numbers = current_numbers + 1 # Increment the appropriate variable
return count
print(even_numbers(25)) # Should print 13
print(even_numbers(144)) # Should print 73
print(even_numbers(1000)) # Should print 501
print(even_numbers(0)) # Should print 1
Evaluation took more than 5 seconds to complete.
Please try again with a simpler expression.
def even_numbers(n):
count = 0
current_number = 0
while n > current_number: # Complete the while loop condition
if current_number % 2 == 0:
count = count + 1 # Increment the appropriate variable
else:
current_number = current_number + 1 # Increment the appropriate variable
return count
print(even_numbers(25)) # Should print 13
print(even_numbers(144)) # Should print 73
print(even_numbers(1000)) # Should print 501
print(even_numbers(0)) # Should print 1
Evaluation took more than 5 seconds to complete.
Please try again with a simpler expression.
def even_numbers(n):
count = 0
current_numbers = 1
while n > current_numbers: # Complete the while loop condition
if current_numbers % 2 == 0:
count = count + 1 # Increment the appropriate variable
current_numbers = current_numbers + 1
else:
current_numbers = current_numbers + 1 # Increment the appropriate variable
return count
print(even_numbers(25)) # Should print 13
print(even_numbers(144)) # Should print 73
print(even_numbers(1000)) # Should print 501
print(even_numbers(0)) # Should print 1
12
71
499
0
Answers:
There’s no else:
in the code in the assignment. You need to increment current_number
every iteration, not just when the number is odd. Otherwise you’ll get stuck on the same number when it’s even. So you should not add an else:
block.
Also, the results should count n
if it’s even, so the condition should be current_number <= n
.
def even_numbers(n):
count = 0
current_number = 0
while current_number <= n: # Complete the while loop condition
if current_number % 2 == 0::
count += 1 # Increment the appropriate variable
current_number += 1 # Increment the appropriate variable
return count
I like your last attempt. You need to start current_number
at 0 and make sure to use a greater than or equal to sign:
def even_numbers(n):
count = 0
current_numbers = 0
while n >= current_numbers: # Complete the while loop condition
if current_numbers % 2 == 0:
count = count + 1 # Increment the appropriate variable
current_numbers = current_numbers + 1
else:
current_numbers = current_numbers + 1 # Increment the appropriate variable
return count
print(even_numbers(25)) # Should print 13
print(even_numbers(144)) # Should print 73
print(even_numbers(1000)) # Should print 501
print(even_numbers(0)) # Should print 1
You are counting even numbers from current_number == 0
to current_number == n
, so you should initialize current_number
to 0
, and the loop condition should be current_number <= n
.
Modifying your third attempt, the code should be:
def even_numbers(n):
count = 0
current_number = 0
while n >= current_number: # Complete the while loop condition
if current_number % 2 == 0:
count = count + 1 # Increment the appropriate variable
current_number = current_number + 1
else:
current_number = current_number + 1 # Increment the appropriate variable
return count
print(even_numbers(25)) # Should print 13
print(even_numbers(144)) # Should print 73
print(even_numbers(1000)) # Should print 501
print(even_numbers(0)) # Should print 1
Finally, the current_number = current_number + 1
line is in both if
and else
branch, so you can extract it out and eliminate else
:
def even_numbers(n):
count = 0
current_number = 0
while current_number <= n: # Complete the while loop condition
if current_number % 2 == 0:
count += 1 # Increment the appropriate variable
current_number += 1 # Increment the appropriate variable
return count
This got me the right answer
def even_numbers(n):
count = 0
current_number = 0
while current_number <= n:
if current_number % 2 == 0:
count += 1
current_number += 1
return count
print(even_numbers(25))
print(even_numbers(144))
print(even_numbers(1000))
print(even_numbers(0))
This works, too, and I would absolutely submit this.
def even_numbers(n):
count = 0
current_number = 0
while not current_number: # Complete the while loop condition
if current_number % 2 == 0:
count += n // 2 + 1 # Increment the appropriate variable
current_number = 1 # Increment the appropriate variable
return count
I am suppose to be able to complete the following in Python…
Fill in the blanks to complete the function “even_numbers(n)”. This function should count how many even numbers exist in a sequence from 0 to the given “n” number, where 0 counts as an even number. For example, even_numbers(25) should return 13, and even_numbers(6) should return 4.
def even_numbers(n): count = 0 current_number = 0 while ___ # Complete the while loop condition if current_number % 2 == 0:: ___ # Increment the appropriate variable ___ # Increment the appropriate variable return count print(even_numbers(25)) # Should print 13 print(even_numbers(144)) # Should print 73 print(even_numbers(1000)) # Should print 501 print(even_numbers(0)) # Should print 1
I’ve tried every variation I can think of for changing every variable I can think of. I have referenced the coursework (I’ve been stuck on this question for a solid week and counting), I have searched Stack Overflow and found multiple variations to reference not exactly what I need to do (and most well advanced from where I am learning right now… even though they solve the problem they don’t…I tried them anyway hoping a light bulb would go off but it hasn’t).
I’ve been trying to get the print(even_numbers(#)) sections to match up… make the script work.
Is there anyone who can explain this to me without giving away the answer outright? I really want to understand where I am going wrong and why. I am a complete novice and mostly self taught from books.
Attempts (a few of them)
def even_numbers(n):
count = 0
current_numbers = 0
while n > current_numbers: # Complete the while loop condition
if current_numbers % 2 == 0:
count = count + 1 # Increment the appropriate variable
else:
current_numbers = current_numbers + 1 # Increment the appropriate variable
return count
print(even_numbers(25)) # Should print 13
print(even_numbers(144)) # Should print 73
print(even_numbers(1000)) # Should print 501
print(even_numbers(0)) # Should print 1
Evaluation took more than 5 seconds to complete.
Please try again with a simpler expression.
def even_numbers(n):
count = 0
current_number = 0
while n > current_number: # Complete the while loop condition
if current_number % 2 == 0:
count = count + 1 # Increment the appropriate variable
else:
current_number = current_number + 1 # Increment the appropriate variable
return count
print(even_numbers(25)) # Should print 13
print(even_numbers(144)) # Should print 73
print(even_numbers(1000)) # Should print 501
print(even_numbers(0)) # Should print 1
Evaluation took more than 5 seconds to complete.
Please try again with a simpler expression.
def even_numbers(n):
count = 0
current_numbers = 1
while n > current_numbers: # Complete the while loop condition
if current_numbers % 2 == 0:
count = count + 1 # Increment the appropriate variable
current_numbers = current_numbers + 1
else:
current_numbers = current_numbers + 1 # Increment the appropriate variable
return count
print(even_numbers(25)) # Should print 13
print(even_numbers(144)) # Should print 73
print(even_numbers(1000)) # Should print 501
print(even_numbers(0)) # Should print 1
12
71
499
0
There’s no else:
in the code in the assignment. You need to increment current_number
every iteration, not just when the number is odd. Otherwise you’ll get stuck on the same number when it’s even. So you should not add an else:
block.
Also, the results should count n
if it’s even, so the condition should be current_number <= n
.
def even_numbers(n):
count = 0
current_number = 0
while current_number <= n: # Complete the while loop condition
if current_number % 2 == 0::
count += 1 # Increment the appropriate variable
current_number += 1 # Increment the appropriate variable
return count
I like your last attempt. You need to start current_number
at 0 and make sure to use a greater than or equal to sign:
def even_numbers(n):
count = 0
current_numbers = 0
while n >= current_numbers: # Complete the while loop condition
if current_numbers % 2 == 0:
count = count + 1 # Increment the appropriate variable
current_numbers = current_numbers + 1
else:
current_numbers = current_numbers + 1 # Increment the appropriate variable
return count
print(even_numbers(25)) # Should print 13
print(even_numbers(144)) # Should print 73
print(even_numbers(1000)) # Should print 501
print(even_numbers(0)) # Should print 1
You are counting even numbers from current_number == 0
to current_number == n
, so you should initialize current_number
to 0
, and the loop condition should be current_number <= n
.
Modifying your third attempt, the code should be:
def even_numbers(n):
count = 0
current_number = 0
while n >= current_number: # Complete the while loop condition
if current_number % 2 == 0:
count = count + 1 # Increment the appropriate variable
current_number = current_number + 1
else:
current_number = current_number + 1 # Increment the appropriate variable
return count
print(even_numbers(25)) # Should print 13
print(even_numbers(144)) # Should print 73
print(even_numbers(1000)) # Should print 501
print(even_numbers(0)) # Should print 1
Finally, the current_number = current_number + 1
line is in both if
and else
branch, so you can extract it out and eliminate else
:
def even_numbers(n):
count = 0
current_number = 0
while current_number <= n: # Complete the while loop condition
if current_number % 2 == 0:
count += 1 # Increment the appropriate variable
current_number += 1 # Increment the appropriate variable
return count
This got me the right answer
def even_numbers(n):
count = 0
current_number = 0
while current_number <= n:
if current_number % 2 == 0:
count += 1
current_number += 1
return count
print(even_numbers(25))
print(even_numbers(144))
print(even_numbers(1000))
print(even_numbers(0))
This works, too, and I would absolutely submit this.
def even_numbers(n):
count = 0
current_number = 0
while not current_number: # Complete the while loop condition
if current_number % 2 == 0:
count += n // 2 + 1 # Increment the appropriate variable
current_number = 1 # Increment the appropriate variable
return count