You can use a for
loop and conditional statement to count up to a certain number and count every even number during the iteration.
main.py165 chars10 lines
In this code, we first take input from the user for the number to count up to. We initialize a count
variable to 0, which we will increment every time we encounter an even number during the loop.
We use a for
loop and range()
function to iterate from 1 up to the input number (num
). During each iteration of the loop, we use a conditional statement (if i % 2 == 0
) to check if the current number is even. If it is, we increment the count
variable. We also use a print()
statement to output the current number.
Finally, after the loop is complete, we use another print()
statement to output the total number of even numbers that were counted.
gistlibby LogSnag