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

19th Jul 2021, 5:11 PM
Cory Peitsch
Cory Peitsch - avatar
5 Respostas
0
Add a condition inside the loop: if x % 2 == 0: print(x) x += 1
19th Jul 2021, 5:16 PM
Yahel
Yahel - avatar
+ 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.
19th Jul 2021, 5:16 PM
Jakko Jak
Jakko Jak - avatar
+ 1
You could also in that example change your iterator so that it counts by every two numbers counting only the even ones.
14th Oct 2021, 8:57 PM
Devery Mcneely
Devery Mcneely - avatar
0
Kept it simple and just altered the iterator. x = 0 while x <= 10: print(x) x = x + 2
13th Dec 2021, 11:13 PM
Daniel White
Daniel White - avatar
- 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
22nd Dec 2021, 11:08 AM
SANVIKA