Question

Practice Activity -- Working with Counting Loops -- Part 2 Objective: to practice writing programs that...

Practice Activity -- Working with Counting Loops -- Part 2

Objective:

  • to practice writing programs that use counting loops

Instructions:

This is the second part of a 2-part practice activity. Be sure to complete Part 1 before doing these exercises.

Write a Python program to solve each of the following in the order shown (in each pattern, the intention is that each number be printed on a separate line; no commas are required):

  • Use a 'while' loop to create this pattern: -3, -2, -1, 0, 1, 2, 3, 4
  • Use a 'for' loop to create the same pattern.
  • Use a 'while' loop to create this pattern: 20, 15, 10, 5, 0, -5, -10
  • Use a 'for' loop to create the same pattern.
  • Use a 'while' loop to create this pattern: 36, 25, 16, 9, 4, 1, 0, 1, 4, 9, 16, 25, 36 (Hint: These are the squares of the numbers from -6 to 6. Use a loop to count -6, -5, -4, ... 6, but write out the square of the number instead of the number itself. (Optional: Use a 'for' loop to create the same pattern.)
  • Use a 'while' loop to create this pattern: 10, 20, 40, 80, 160, 320, 640 (Hint: Can you see what the pattern is? You can use any statement that changes the value of the loop control variable as an update statement in a 'while' loop. How does this value get updated with each step in the pattern?)

Feel free to discuss any of these in the Discussion Forum. Upload your completed work as a single .py file with each of the exercises above completed in the order listed.


Homework Answers

Answer #1

.I have implemented the required code in python.

And also provided the output of the respective code.

Source code:

Loopspart-2.py

# while loop to print -3, -2, -1, 0, 1, 2, 3, 4
i = -3
while i < 5:
  print(i)
  i += 1

# for loop to print -3, -2, -1, 0, 1, 2, 3, 4
x = range(-3, 5)
for n in x:
  print(n)

# while loop to print 20, 15, 10, 5, 0, -5, -10
i = 4
while i > -3:
  print(i*5)
  i -= 1

# for loop to print 20, 15, 10, 5, 0, -5, -10
start = 20
stop = -15
step = -5
for number in range(start, stop, step):
    print(number)

# while loop to print 36, 25, 16, 9, 4, 1, 0, 1, 4, 9, 16, 25, 36
i = -6
while i <= 6:
  print(i**2)
  i += 1
  
# while loop to print 10, 20, 40, 80, 160, 320, 640  
i=1
while i <=64 :
  print(i*10)
  i += i

Output:

-3
-2
-1
0
1
2
3
4
-3
-2
-1
0
1
2
3
4
20
15
10
5
0
-5
-10
20
15
10
5
0
-5
-10
36
25
16
9
4
1
0
1
4
9
16
25
36
10
20
40
80
160
320
640
Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Part A Poker Hands: In this activity, we will apply some of the various counting techniques...
Part A Poker Hands: In this activity, we will apply some of the various counting techniques that we have studied including the product and sum rules, the principle of inclusion-exclusion, permutations, and combinations. Our application will be counting the number of ways to be dealt various hands in poker, and analyzing the results. First, if you are not familiar with poker the following is some basic information.   These are the possible 5-card hands: Royal Flush (A,K,Q,J,10 of the same suit);...
4.9.1: Nested loops: Indent text. JAVA Print numbers 0, 1, 2, ..., userNum as shown, with...
4.9.1: Nested loops: Indent text. JAVA Print numbers 0, 1, 2, ..., userNum as shown, with each number indented by that number of spaces. For each printed line, print the leading spaces, then the number, and then a newline. Hint: Use i and j as loop variables (initialize i and j explicitly). Note: Avoid any other spaces like spaces after the printed number. Ex: userNum = 3 prints: 0 1 2 3 Please use my template import java.util.Scanner; public class...
In this exercise we will practice using loops to handle collections/containers. Your job is to write...
In this exercise we will practice using loops to handle collections/containers. Your job is to write a program that asks the user to enter a sentence, then it counts and displays the occurrence of each letter. Note: your program should count the letters without regard to case. For example, A and a are counted as the same. Here is a sample run: Enter a sentence: It's a very nice day today! a: 3 times c: 1 times d: 2 times...
Create a new file named quiz_loops.py. b. Write your name and date in a comment. c....
Create a new file named quiz_loops.py. b. Write your name and date in a comment. c. Write a for loop which will display this set of values 2,4,6,8,10,12,14,16. d. Write a while which will display this set of values 16,13,10,7,4,1,-2. e. Write a for loop which will print ‘Loops are fun’ three times. f. Write a while loop that will prompt you for four numbers. Convert the number to an integer. Compute the power as power = power ** number....
The Lab experiment of a vibrating string was modified to be a 90-cm aluminum string joined...
The Lab experiment of a vibrating string was modified to be a 90-cm aluminum string joined to an 129.9 cm steel string. The vertical pulling mass was 10 kilograms over a pulley. The standing wave pattern had 4*Y loops in the aluminum section and n loops in the steel section. One loop represents half wavelength etc in the Lab Book drawings. There were 4*Y + n loops. There were 4*Y + n + 1 nodes, counting the end points. Y...
# Python Given the list values = [], write code that fills the list with each...
# Python Given the list values = [], write code that fills the list with each set of numbers below. Note: use loops if it is necessary 1 2 3 4 5 6 7 8 9 10 0 2 4 6 8 10 12 14 16 18 20 1 4 9 16 25 36 49 64 81 100 0 0 0 0 0 0 0 0 0 0 1 4 9 16 9 7 4 9 11 0 1 0...
Instead of a graphing activity, this week we will do a practice problem. Use the details...
Instead of a graphing activity, this week we will do a practice problem. Use the details below to find the optimal consumption bundle: how many hot dogs and how many drinks will maximize utility for $10 in spending? Income=$10    Price of a hot dog= $2   Price of a drink= $1 Hot Dogs MU hot dog MU/P Drinks MU drink MU/P 1 20 1 20 2 16 2 15 3 10 3 10 4 6 4 5 5 3 5 3...
In C# using the Console App (.NET FRAMEWORK) Create and test a Windows Console application that...
In C# using the Console App (.NET FRAMEWORK) Create and test a Windows Console application that displays the following patterns separately, one after the other. You MUST use nested for loops to generate the patterns, like the example in the PowerPoint slides from this chapter. All asterisks (*) should be displayed by a single statement of the form Console.Write("*"); which causes the asterisks to display side by side. A statement of the form Console.WriteLine(); can be used to move to...
PLEASE CODE EVERYTHING IN C++ Quick Thinking: Arrays Objective: Test your knowledge in providing efficient solutions...
PLEASE CODE EVERYTHING IN C++ Quick Thinking: Arrays Objective: Test your knowledge in providing efficient solutions for operations on arrays. What you can use only: Loops These variables only const int SIZE = 4; int variable = 0, master array [SIZE][SIZE]; double average = 0.0, parallel array [SIZE]; One conditional statement per problem if needed Note: the conditional statement cannot contain an “else” or “else if’ Provide solutions to the following problem. Absolutely no hard coding can be used. ----------------------------------------------------------------------------------------------------------------------------------------------------------...
Arrays, loops, functions: Lotto Element Repeated Function Write a function that that takes as parameters an...
Arrays, loops, functions: Lotto Element Repeated Function Write a function that that takes as parameters an array of ints, an int value named element, and an int value named end. Return a bool based on whether the element appears in the array starting from index 0 and up to but not including the end index. Generate Random Array Write a function that takes as parameters an array of integers and another integer for the size of the array. Create a...