Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Can someone help me solve this question in Python? Task 2: for-Loop Basics The template for a for-loop is for in : Kbody> where each

Can someone help me solve this question in Python?image text in transcribedimage text in transcribed

Task 2: for-Loop Basics The template for a for-loop is for in : Kbody> where each element in is used once each time the loop body is executed. On each pass of the loop this element is assigned to . In Python there are a variety of iterables, including lists, tuples, and strings. When the first pass of the loop is initiated, a list's first element is assigned to . When the second pass is initiated, its second element is assigned to and so on until all the elements have been used and the looping ends. serves as the 'loop variable.' Any valid identifier can be used for . In some loops we're interested in using in the body of the loop while in other loops this value is ignored (in such loops is typically used as a counter for which there may not be a need) For this task, you'll use for-loops in two different ways. You'll use a counter and the items in the list itself, and you'll use an accumulator. Note, however, that it's important to remember that for-loops can be used with any iterable, not just lists! We'll again be using the IDLE Shell window interactive environment. First create an empty list called numbers. Then initialize a counter i by assigning it to a value of 1. Next write a for-loop that uses fave as the loop variable and your top.ten list as the iterable. The output of your code should look similar to the following (but with your favorite items, not mine). For each pass of the loop, append the counter to the numbers list and then use augmented assignment as discussed in class to increment the value of the counter 1 1. almond croissants 2 2. art 3 3. cookies 4 4. cruises s 5. dark chocolate 6 6. espresso 7 7. 1ong, sunny summer days 8. reading 9 9. sleeping 10 10. working out Next, use a for-loop to sum the numbers in your numbers list and then find the average usin;g the len) function in the denominator. Use augmented assignment to add to your accumulator. Don't forget to initialize your accumulator to O! Your answer should be 5.5

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Management System MCQs Multiple Choice Questions And Answers

Authors: Arshad Iqbal

1st Edition

1073328554, 978-1073328550

More Books

Students also viewed these Databases questions