Reputation: 45
myList = []
size = int(input("How many integers do you want in your list? "))
for list in range(size):
element = int(input("Enter an integer to add to your list: "))
myList.append(element)
print(myList)
#a function that keeps count of all even integers using while loop
def count_evens_while(alist):
evenIntegers = 0
while range <= size:
for element in range(size):
if element % 2 == 0:
evenIntegers = evenIntegers + 1
print(evenIntegers)
This is what I have so far. I can't figure out what I need to do! (I'm a beginner, so sorry if this question seems so simple/easy to fix)
Upvotes: 0
Views: 1941
Reputation: 22953
You have several problems here:
size
in the head of the while
loop.range
builtin class as an integer value which doesn't make sense. alist
in your function.for
loop in your while
because you are not trying to find the amount of even numbers for each number between 1
to element
, your trying to find all the even numbers in alist
(presumably).Here is what the above fixes would look like:
def count_evens_while(alist):
evenIntegers = 0
size = 0
while size < len(alist):
if alist[size] % 2 == 0:
evenIntegers = evenIntegers + 1
size = size + 1
print(evenIntegers)
In the first part of the while
loop:
size < len(alist)
We are telling Python to comparse the value of size
to the length of alist
each time we loop. We then incrmenet size
by one each time in the loop:
size = size + 1
This basically means that each iteration of the loop, the value of size
will correspond to an index of alist
. The loop will stop when size
reaches the last index of alist
. Here's a visual example to help explain. Assume alist
is [1, 2, 3]
and size
initially equals 0
:
First iteration
alist = [1, 2, 3]
^
|
+--- size = 0
Second iteration
alist = [1, 2, 3]
^
|
+--- size = 1
Third (and last) iteration
alist = [1, 2, 3]
^
|
+--- size = 2
The next important part:
alist[size] % 2 == 0
Means we are indexing the list alist
. When you index a list, you use an integer to get a value from the position that the integer corresponds to. In this case, alist[size]
means that we are getting the value of the integer at position of the value of size
. The last part:
% 2 == 0
Means that we are testing if the integer at position size
in alist
is even. If it is, we increment the count of even numbers, if not we do nothing.
There are some improvements you can make to your current solution
for
loop instead of a while
loop. It would be the more natural solution here.evenIntegers = evenIntegers + 1
. You an use the increment operator, +=
, instead.Here are the improvements from above applied to your code:
def count_evens_while(alist):
evenIntegers = 0
for element in alist:
if element % 2 == 0:
evenIntegers += 1
print(evenIntegers)
However, this can be improved more! You don't need a for
loop. You can use a generator comprehension combined with the sum()
builtin function to count all of the elements in alist
that are even:
def count_evens_while(alist):
return sum(1 for el in alist if el % 2)
The last thing I would critique on your code would be to use better variable names. Use names that accurately describe what the variable represents.For example, the variable myList
would be better renamed to user_input_numbers
.
Upvotes: 0
Reputation: 11922
This is very unlike your program but I hope to teach you something with it... First of all if all you need is to "keep count" you don't really have to save the integers themselves in a list. Which means that all you really need is something like this:
count = 0
while True:
element = int(input("Enter an integer to add to your list: "))
if element % 2 == 0:
count += 1 # which is the same as count = count + 1
elif element == 9999:
break # user could enter 9999 to exit the loop
print('I have counted ' + str(count) + ' even numbers so far')
I hope in this just to show you that you don't have to complicate simple things
Upvotes: 0
Reputation: 1420
def count_evens_while(alist):
evenIntegers = 0
idx = 0
while idx < len(alist):
if alist[idx] % 2 == 0:
evenIntegers = evenIntegers + 1
idx += 1
print(evenIntegers)
Using a for loop would be much simpler though:
def count_evens_while(alist):
evenIntegers = 0
for el in alist:
if el % 2 == 0:
evenIntegers = evenIntegers + 1
print(evenIntegers)
And even simpler using some list comprehension:
def count_evens_while(alist):
evenIntegers = sum(1 for x in alist if (x%2 == 0))
print(evenIntegers)
Upvotes: 1
Reputation: 117
You do not need a while
loop at all:
myList = []
size = int(input("How many integers do you want in your list? "))
for list in range(size):
element = int(input("Enter an integer to add to your list: "))
myList.append(element)
print(myList)
#a function that keeps count of all even integers using while loop
def count_evens_while(alist):
evenIntegers = 0
for element in alist:
if element % 2 == 0:
evenIntegers = evenIntegers + 1
return evenIntegers
print(count_evens_while(myList))
If you need to stop a for
loop early, you can use the break
statement to break out of the loop. Otherwise, it will stop by itself once it reaches the end of the list. It does not need an ending condition to be given manually.
Upvotes: 0