Deal of The Day! Hurry Up, Grab the Special Discount - Save 25% - Ends In 00:00:00 Coupon code: SAVE25
Welcome to Pass4Success

- Free Preparation Discussions

WGU Foundations of Programming Python Exam - Topic 2 Question 5 Discussion

A program needs to update all values in a list by adding 5 to each number. Which for loop structure accomplishes this task?
B) for i in range(len(numbers)):numbers[i] = numbers[i] + 5
A) for number in numbers:number = number + 5
C) for numbers in number:numbers = numbers + 5

WGU Foundations of Programming Python Exam - Topic 2 Question 5 Discussion

Actual exam question for WGU's Foundations of Programming Python exam
Question #: 5
Topic #: 2
[All Foundations of Programming Python Questions]

A program needs to update all values in a list by adding 5 to each number. Which for loop structure accomplishes this task?

Show Suggested Answer Hide Answer
Suggested Answer: B

To update the actual values inside a list, the program must update each value by itsindex.

The correct code is:

for i in range(len(numbers)):

numbers[i] = numbers[i] + 5

Here, range(len(numbers)) produces valid index positions for the list. The variable i is used to access and update each list element.

Example:

numbers = [10, 20, 30]

for i in range(len(numbers)):

numbers[i] = numbers[i] + 5

print(numbers)

Output:

[15, 25, 35]

Option A does not correctly update the original list:

for number in numbers:

number = number + 5

This changes only the temporary loop variable number, not the actual elements stored inside the list.

Therefore, the correct answer isB.


Contribute your Thoughts:

0/2000 characters
Elouise
7 hours ago
I remember we did something similar in class, and I think using range with the length of the list is the right approach, so B makes sense to me.
upvoted 0 times
...
Denise
5 days ago
I'm not entirely sure, but I feel like option A might not work since it doesn't actually update the list. It just changes the loop variable.
upvoted 0 times
...
Aliza
11 days ago
I think option B is correct because it uses the index to access each element in the list. That seems familiar from the practice questions.
upvoted 0 times
...
Douglass
16 days ago
Option C looks wrong to me, but I can't quite recall why. It just feels off, like it's mixing up the variable names.
upvoted 0 times
...
Bambi
2 months ago
I remember we did a similar question in class, and I think using range with the length of the list is the right approach, so B sounds good.
upvoted 0 times
...
Catherin
2 months ago
I'm not entirely sure, but I feel like option A might not work since it doesn't actually modify the list. It just changes the variable.
upvoted 0 times
...
Zona
2 months ago
I think option B is correct because it uses the index to access each element in the list. That seems familiar from our practice questions.
upvoted 0 times
...

Save Cancel