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

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
Bambi
17 days 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
22 days 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
27 days 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