0
Python Core - 26.2 Practice "Call It Even"
I'm not sure what to do here: You are given a program that outputs all the numbers from 0 to 10. Change the code to make it output only the even numbers. Any integer that can be divided exactly by 2 is an even number. x = 0 while x <= 10: print(x) x += 1
5 Respostas
0
Add a condition inside the loop:
if x % 2 == 0:
print(x)
x += 1
+ 1
You'll want to create a condition inside of the loop that's dividing x by 2 and seeing if there's a remainder or not. IF the remainder is 0 THEN it's an even number, ELSE it's an odd number. So if it's even, print the variable, ELSE increase x by 1 without printing and let it loop again.
Hope that helps explain it a bit.
+ 1
You could also in that example change your iterator so that it counts by every two numbers counting only the even ones.
0
Kept it simple and just altered the iterator.
x = 0
while x <= 10:
print(x)
x = x + 2
- 1
code 1:
i=0
while(i<=10):
if(i%2==0):
print(i)
i=i+1
or
code 2:
i=0
while(i<=10):
print(i)
i=i+2