Forum in maintenance, we will back soon 🙂
Loops quiz
Â
I asked chat gpt but not sure it was right.
Â
Give me the answer for it.
Â
Still not sure how I use and ask chat gpt.
Â
So far not easy course to me..
Hi friend, no worries, we all started like this, I am here to help you understand.Â
here is a sample code:
# List of numbers numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] # Loop through the list for number in numbers: # Check if the number is even if number % 2 == 0: print(f'{number} is even') else: print(f'{number} is odd')
Â
This program iterates over every number in the numbers
list and checks if it is even by using the modulus operator (%
). If the remainder when the number is divided by 2 is 0, then the number is even. If the remainder is anything else (in this case it can only be 1), the number is odd.
Â
Does this help?
Â
mmm, let me try once again.
imagine you have a bag containing 10 balls. on each ball, there is a number written. (1 to 10)
now we want to classify the balls into two other bags, one coating balls with odd numbers, and the other with even numbers.
so we pick up the first ball, and we look at the number, for example we find it is 5. we want to know now if 5 is odd or even.
what we do is divide 5 by 2 (5/2)
we get 2, and the remainder 1. (remember grade 3 decision)
so if we have a remainder, the number is odd, if the remainder is 0, the number is even, so 5 is odd.
Â
we repeat the process on all balls in the bag till we finish.
Â
does this make sense?
Â
Â
Â
Â