 # Can I combine a `while` and a `for` loop?

if the list was empty, your code would throw an error:

``````def max_num(nums):
maximum=nums
for num in range(len(nums)):
if nums[num]>maximum:
maximum=nums[num]
return maximum

max_num([])
``````

and using `range()` seems redundant, you only need the values from the list, not the indices, so you could just do:

``````for num in nums:
``````

somethings like this, it seems work?

``````def max_num(nums):
maximum=nums
for num in nums:
if num>maximum:
maximum=num
return maximum
``````

or maybe somethings like this:

``````def max_num(nums):
if len(nums)==0:
print("List is empty")
else:
maximum=nums
for num in nums:
if num>maximum:
maximum=num
return maximum
``````

with inspiration from previous posts, i came up with this:

``````def over_nine_thousand(lst):
#initialize isitover
isitover = 0
#check for a list with items in it
if len(lst) > 0:
#iterate through list
for item in lst:
#check if it is already over 9000
if isitover <= 9000:
isitover += item
#else
#isitover still = 0
return isitover
``````

What is the point of having a while-loop if you can just use a for-loop with an indented if-statement? I’m trying to figure out when best to use ‘for’ and ‘while’.

while loops are useful when you don’t know how many iterations your loop will make. Lets say i want user input, then validate the user input before continuing with the rest of the program, i could do:

``````while True:
user_input = input('enter something:')
is_valid(user_input):
break
``````

with a for loop, this is a lot more difficult. And surely there are other cases where a while loop is useful

``````
def over_nine_thousand(lst):
i = 0
if sum(lst) < 9000:
return sum(lst)
if lst == []:
return 0
i += 1
break

``````

The editor keeps failing to run my code, and I am unable to advance to the next level. I would appreciate some assistance with what I might be doing wrong.

Thanks

Several problems, have you tried some function calls to test your function? Also, can you give me the exercise url? I can’t find the exercise url in the topic

here:

``````i += 1
``````

you increase `i`, but you never calculate the sum again.

this:

``````      break
``````

is also problematic, `break` will break the loop. So your code never gets to execute `return`

Thanks for the response.

It’s the over 9000 loop exercise.

Over 9000

``````
def over_nine_thousand(lst):
i = 0
if sum(lst) < 9000:
return sum(lst)
if lst == []:
return 0
i += 1
sum(lst[:i])

``````

I’ve updated the code as per your advise, but it still won’t run

then you still have an infinity loop. How could that be?

Ah, never mind. I finally got it going by removing the answer variable assignment in line 3, and Substituting sum(lst[:i]) For answer everywhere else.

Thank you.

def over_nine_thousand(lst):
(ind)summ = 0
(ind+)while summ <= 9000:
(ind++++) summ += (lst.pop(0))

(ind+)return summ

#got the expected result for this lesson 9020 for the above code…but still the answer was not approved by lesson IDE,saying the bellow error

#pop from empty list

the lesson displays a short version of the error message. What if you list of which the sum is less then 9000? Something like this:

``````def over_nine_thousand(lst):
summ = 0
while summ <= 9000:
summ += (lst.pop(0))

return summ

#got the expected res
over_nine_thousand([100, 200, 300])
``````

then you would try to pop from an empty list, which gives an error

Couldn’t you omit the ‘if len(lst) > 0’ because it will not matter since we are returning zero nonetheless?

My reply doesn’t pertain to combining a while loop and a for loop but I wanted to share the while loop I used. I choose a while loop because the suggested use of one over a for loop is when the number of iterations isn’t known ahead of time, however, I understand the logic of the for loop that was used in the answer. Anyway, here is my solution:

``````def over_nine_thousand(lst):
total, i = 0, 0
while i < len(lst) and total <= 9000:
total += lst[i]
i += 1
``````
1 Like

Both these conditions can be covered by a while loop, in fact, there are while loop solutions in this topic. So you are not forced to use for loop (which your answer seems to imply)

1 Like