Question

(Write or type in answers, except for problem #15, which should be entered as a small program, tested, and submitted in Repl.11. Trace (show the results each time a statement executes) the following code, if the user provides an input value of 5. rep

0 0
Add a comment Improve this question Transcribed image text
Answer #1

1) Python statement to define a list

Statement:

temps=[95,100,77,54,103,82]

Screenshot of the code:

1 2 temps=[95,100,77,54,103,82]

2)length of the list temps

The length of the list temps is 6.

Code:

temps=[95,100,77,54,103,82]
length=len(temps)
print(length)

Screenshot of the code:

1 2 3 4 temps=[95,100,77,54,103,82] length=len(temps) print(length)

Screenshot of the output:

In [2]: runfile(C:/Users/Sandhya/Spyder programs/s.py, wdir=C:/Users/Sandhya/Spyder programs) 63)index position value of 77

Code:

temps=[95,100,77,54,103,82]
#finding index 
i=temps.index(77)
print(i)

Screenshot of the code:

1 2 3 4. 5 temps=[95, 100,77,54,103,82] #finding index i=temps.index(77) print(i)Screenshot of the output:

In [4]: runfile(C:/Users/Sandhya/Spyder programs/s.py, wdir=C:/Users/Sandhya/Spyder programs) 2Index position value of 77 is 2.

4)Output of print(temps)

print(temps) will display the list temps

Code:

temps=[95,100,77,54,103,82]
print(temps)

Screenshot of the code:

1 2 3 temps=[95,100,77,54,103,82] print(temps)Screenshot of the output:

In [6]: runfile(C:/Users/Sandhya/Spyder programs/s.py, wdir=C:/Users/Sandhya/Spyder programs) [95, 100, 77, 54, 103, 82]5) Show output of print(temps[3])

It displays the element which is present at index position 3.

Code:

temps=[95,100,77,54,103,82]
i=temps[3]
print(i)

Screenshot of the code:

1 2 3 4 temps=[95, 100,77,54,103,82] i=temps [3] print(i)Screenshot of the output:

In [7]: runfile(C:/Users/Sandhya/Spyder programs/s.py, wdir=C:/Users/Sandhya/Spyder programs) 54The value at index position 3 is 54.

6) Output of print(temps[2:5])

It displays the elements from index position 2 to (5-1) that is 2 to 4

Code:

temps=[95,100,77,54,103,82]
print(temps[2:5])

Screenshot of the code:

1 2 3 temps=[95,100,77,54,103,82] print(temps [2:5])Screenshot of the output:

In [8]: runfile(C:/Users/Sandhya/Spyder programs/s.py, wdir=C:/Users/Sandhya/Spyder programs) [77, 54, 103]7) statement to add another temperature at the end of the list

temps=[95,100,77,54,103,82]
#adding value at the end of the list
temps.append(39)
#printing the list
print(temps)

Screenshot of the code:

1 2 3 4. 5 6 temps=[95, 100,77,54,103,82] #adding value at the end of the list temps.append(39) #printing the list print(tempScreenshot of the output:

In [9]: runfile(C:/Users/Sandhya/Spyder programs/s.py, wdir=C:/Users/Sandhya/Spyder programs) [95, 100, 77, 54, 103, 82,8) for loop to print the list:

Code:

temps=[95,100,77,54,103,82]
for i in temps:
    print(i)

Screenshot of the code:

1 2 3 4 temps=[95,100,77,54,103,82] for i in temps: print(i)

Screenshot of the output:

In [10]: runfile(C:/Users/Sandhya/Spyder programs/s.py, wdir=C:/Users/Sandhya/Spyder programs) 95 100 77 54 103 82

9) using iteration dispaly the temp>100

Code:

temps=[95,100,77,54,103,82]
for i in temps:
    if(i>100):
        print(i)

Screenshot of the code:

1 2 3 4. 5 temps=[95,100,77,54,103,82] for i in temps: if(i>100): print(i)

Screenshot of the output:

In [11]: runfile(C:/Users/Sandhya/Spyder programs/s.py, wdir=C:/Users/Sandhya/Spyder programs) 10310) avg of the values in temp

temps=[95,100,77,54,103,82]
#finding the length of the list
n=len(temps)
sum=0
#for loop to find the sum pf all values
for i in temps:
    sum=sum+i
#finding the average
avg=sum/n
#displaying average
print(avg)

Screenshot of the code:

1 2 3 5 temps=[95, 100,77,54,103,82] #finding the length of the list n=len(temps) sum=0 #for loop to find the sum pf all valuScreenshot of the output:

In [13]: runfile(C:/Users/Sandhya/Spyder programs/s.py, wdir=C:/Users/Sandhya/Spyder programs) 85.1666666666666711) Trace when the user inputs a value of 5.

Code:

reps=int(input("Enter the number of times you want to display Meteors!"))
n=2
while n<reps:
    print("Meteors!")
    n=n+1

Tracing:

n=2, reps=5

The code in the while loop executes when n<reps

i)n=2,reps=5

while(2<5):

The condition is true . So print Meteors!

n=n+1

n=2+1

n=3

ii)

n=3,reps=5

while(3<5):

The condition is true . So print Meteors!

n=n+1

n=3+1

n=4

iii)

n=4,reps=5

while(4<5):

The condition is true . So print Meteors!

n=n+1

n=4+1

n=5

iv)

n=5,reps=5

while(5<5):

The condition is false. So while loop is terminated.

The word is printed 3 times

12) MCQ

The underlined expression in red is Loop Continuation test.

It checks whether the loop to be continued or stopped.

If the expression is true then the loop continues .

If the expression is false then the loop stops or terminates

13) for loop to define list:

Code:

temps=[i for i in (95,100,77,54,103,82)]
print(temps)

Screenshot of the code:

1 2 3 temps=[i for i in (95,100,77,54,103,82)] print(temps)Screenshot of the output:

In [18]: runfile(C:/Users/Sandhya/Spyder programs/s.py, wdir=C:/Users/Sandhya/Spyder programs) [95, 100, 77, 54, 103, 82]14)Answer:

We can't say how many times the loop repeats. I will repeat until the entered is greater than or equal to 20.

When the value is greater than or equal to 20 the loop stops.

Add a comment
Know the answer?
Add Answer to:
(Write or type in answers, except for problem #15, which should be entered as a small...
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Overview Module 3 Assignment 1 features the design of a pseudocode and a Python program that...

    Overview Module 3 Assignment 1 features the design of a pseudocode and a Python program that uses iteration to guess a number from 1 to 10. Each lab asks you to write pseudocode that plans the program’s logic before you write the program in Python and to turn in three things: 1) the pseudocode, 2) a screenshot of the output, and 3) the Python program. Instructions Write pseudocode for a Python program that uses iteration to guess a number from...

  • in c++ language 1.Re-write the following for loop statement by using a while loop statement: int...

    in c++ language 1.Re-write the following for loop statement by using a while loop statement: int sum = 0; for(int i=0;i<=1000;i++){                 sum+=i; } 1 (b). Following is the main () function of a program. The program asks a user to enter 150 integers and print the largest integer user entered. int main() {    int score, highest;             //missing portion of the program             return 0;    } 1(c). Find the missing portion of the following code, so the...

  • 21 Write a program that asks the user to input the length and breadth of a...

    21 Write a program that asks the user to input the length and breadth of a soccer field, and then computes and returns the number of square meters of grass required to cover the field The formula for the area is the product of length and breadth (5) 22 The following program uses the break statement to terminate an infinite while loop to print 5 numbers Rewrite the program to use a while loop to display numbers from 1 to...

  • Write the code in Python. The output should be exact the same as the given.

    Write the code in Python. The output should be exact the same as the given. Question 1. Study the examples below carefully and write a program for journal subscription Your program should work exactly as the following examples The text in bold indicates user input. Example 1: The user is a student, and the user subscribes to 2 journals Question 1 - Journal of Commodity Markets Journal of Sustainable Mining Journal of Asia-Pacific Biodiversity Student $21 $22 $27 Non-student $50...

  • Write a Java application program that plays a number guessing game with the user. In the...

    Write a Java application program that plays a number guessing game with the user. In the starter code that you are given to help you begin the project, you will find the following lines of code: Random generator = args.length == 0 ? new Random() :                    new Random(Integer.parseInt(args[0])); int secret = generator.nextInt(100); You must keep these lines of code in your program. If you delete these lines, or if you change thse lines in any way, then your program...

  • I need help with a C++ assignment: Write a program containing the following: 1. Variable Definitions...

    I need help with a C++ assignment: Write a program containing the following: 1. Variable Definitions only as (DieRoll, Guess, cnt1, cnt2) followed by this statement: srand((unsigned int)time (NULL)); which will give the random number generator a random starting point. Note: srand and rand require the TIME.H (or iomanip) cnt1 and cnt2 will be used in Chapter 5 drop box as counters for loops. Do NOT create additional variables. Points will be taken off for any additional variable creation. 2....

  • 1) Translate the following equation into a Python assignment statement 2) Write Python code that prints...

    1) Translate the following equation into a Python assignment statement 2) Write Python code that prints PLUS, MINUS, O ZERO, depending on the value stored in a variable named N. 3) What is printed by: 3 - 1 while 5: while 10 Print ) Page 1 of 9 4) Write a Python while loop that reads in integers until the user enters a negative number, then prints the sum of the numbers. 1-2 of 9 4) Write a Python while...

  • Consider the following function with a real variable, x: ?(?) = ?3 - 3?2 + 6?...

    Consider the following function with a real variable, x: ?(?) = ?3 - 3?2 + 6? + 10 a. Write a Python function for the derivative of f(x) that takes x and returns the derivative of f(x). Take the derivative of f(x) analytically with respect to x before writing the function. b. Write a Python code that approximately finds the real root, x0, of f(x) such that f(x0)~0 using the Newton-Raphson method. The code is expected to get an initial...

  • please code in python 1. Write a program to request the user enter a desired number...

    please code in python 1. Write a program to request the user enter a desired number of values. Create a loop to load the desired number of user-specified values into a list. Create two lists with the same values but generated in different ways. On list will be originally initialized to have the desired length before entering the loop. The other list will begin empty and values be appended for every iteration of the loop. Output from the program should...

  • Please use python to write the simple program. Exercise 2 - Summation Write a program to...

    Please use python to write the simple program. Exercise 2 - Summation Write a program to accept integer inputs until the user inputs a non-integer. Then, the program prints the summation of the inputted numbers. Hint: Use the function input() to get the user input. Use a variable total for calculating the summation. Need to use while loop for the repeated inputs. Use if statement with isdigit() function to check whether the user input is an integer or not. if...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT