3 posts were split to a new topic: Stuck in an infinite loop
my inefficient-but-it-works solve
def divisible_by_ten(nums):
i = 0
for num in nums:
if num % 10 == 0:
i = i + 1
return i
More elegant than mine!
Hi guys,
I’m sure i’m going about it it the wrong way but if anyone can give me insights into what’s happening with the code, i’d greatly appreciate it.
From my understanding, my code (at the bottom) should iterate through the entire list however it seems to stop after the first one, can anyone advise as to why (or if it’s doing anything else)?
def divisible_by_ten(nums):
count = 0
for number in nums:
if (number % 10 == 0):
count += 1
return count
#Write your function here
def divisible_by_ten(num):
count1 = len(num)
print(count1)
for numbers in list(range((count1))):
print(list(range(count1)))
print(num)
if num[numbers]%10!=0:
continue
new_list =[ ]
new_list.append(num[numbers])
print(new_list)
return len(new_list)
I recommend running your code here:
http://www.pythontutor.com/visualize.html#mode=edit
to help you to identify the issue. This allows you to step through your code. You might need to add a function call.
thank you! I’ve used it a fair bit
Still have question left? Or is everything crystal clear now?
#Write your function here
def divisible_by_ten(nums):
list = []
for num in nums:
if num % 10 == 0:
list.append(num)
return len(list)
#Uncomment the line below when your function is done
print(divisible_by_ten([20, 25, 30, 35, 40]))
Could someone explain to me if this is an advisable solution to this code challenge
That depends, if you are only interested in how many numbers are divisible by 10, and not which numbers that are, i wouldn’t use a list
i would simply use:
count = 0
then increase count every time your if condition is true
Hello
I dont understand why im only getting 1 in the count as oppossed to 3.
can someone please give me a hand
here is the code
The assignment operators always place the operation ahead of the assignment.
+=
-=
*=
/=
//=
%=
**=
and so on.
Thanks!
Ok, so I changed that part of the code, but im still getting 1 as opposed to 3
why is it only runing the loop once?
again, here is the code
Should return
be inside the loop block?