Need assistence with my python question thats trying to make me print the sum of numbers while using a while loop
Question:
Need help solving a python question, i was given a list of integers [2,3,5,12,44,66] and was asked to access this list and print the sum of those integers that are greater than or equal to 11 while using a while loop
Answers:
So you want to keep a variable outside of your loop to use as the sum. As you iterate over each number, check it and if it is >= 11 you add it to the sum.
As for the loop portion, you need to use a sentinel value to manage when the loop should end. Typically when iterating over lists, you start at 0 and count up by one until the number is the length of the array. From there, in the loop you can use that index to access the item in the list and check its value.
Heres some pseudocode to give you an idea of what i mean.
integer variable 'sum' initialised to 0
integer variable 'sentinel' initialised to 0
while the sentinel variable is less than the size of the list do:
if the value of the number in the list at index 'sentinel' >= 11:
add the value to the sum variable.
end if
increment the sentinal value by one
end while
You can give it a try.
arr = [2,3,5,12,44,66] # Array initialization.
res = 0 # total sum will be stored in this variable.
ct = 0 # set a counter to increment at each iteration.
while(ct <len(arr)): # Start the loop
if(arr[ct] >=11): # if the element at index ct in array is larger or equal to 11,
res = res + arr[ct] # add it to result.
ct = ct+1 # increment counter to iterate each element in array.
print(res)
Please keep me posted 🙂
Need help solving a python question, i was given a list of integers [2,3,5,12,44,66] and was asked to access this list and print the sum of those integers that are greater than or equal to 11 while using a while loop
So you want to keep a variable outside of your loop to use as the sum. As you iterate over each number, check it and if it is >= 11 you add it to the sum.
As for the loop portion, you need to use a sentinel value to manage when the loop should end. Typically when iterating over lists, you start at 0 and count up by one until the number is the length of the array. From there, in the loop you can use that index to access the item in the list and check its value.
Heres some pseudocode to give you an idea of what i mean.
integer variable 'sum' initialised to 0
integer variable 'sentinel' initialised to 0
while the sentinel variable is less than the size of the list do:
if the value of the number in the list at index 'sentinel' >= 11:
add the value to the sum variable.
end if
increment the sentinal value by one
end while
You can give it a try.
arr = [2,3,5,12,44,66] # Array initialization.
res = 0 # total sum will be stored in this variable.
ct = 0 # set a counter to increment at each iteration.
while(ct <len(arr)): # Start the loop
if(arr[ct] >=11): # if the element at index ct in array is larger or equal to 11,
res = res + arr[ct] # add it to result.
ct = ct+1 # increment counter to iterate each element in array.
print(res)
Please keep me posted 🙂