Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Part 1: Using Idle Write a Python Script that Does the Following 1. At the top of your program, import the math library as in

Part 1: Using Idle Write a Python Script that Does the Following

1. At the top of your program, import the math library as in

from math import *

to make the functions in the math module available.

Create a variable and assign into it a constant positive integer number of your choice.

The number should be at most 10.

1

Suppose we call this variable x for this writeup document

Try something like

x = 4

2. Create another variable and assign into it the constant length of your last name (you as the programmer).

You could use for example

lnlen = 6

if the last name were 'Python'

Another way is to compute the length as in

lnlen = len('Python')

Suppose we call the variable holding the length y for this writeup document

3. Create another variable and set its value to 0.

You will later use this variable will hold a sum of some numbers.

Suppose we call this variable mysum for this writeup document

4. Use a Python function to raise x to the power y and store the result in a variable.

Print the result in an informative message

Calling the pow function as in

mypower = pow (a, b)

will compute the a to the power b and cause the result to be stored in the variable named mypower

5. Write a definite loop that will produce the result of x multiplied into y, using the accumulator pattern

The loop should store the value in the variable myproduct.

Accumulation means to build up a result by performing a set of repeated, smaller steps.

Do this by adding x into a sum variable, y times, in a loop

You must determine the result using accumulation, not arithmetic outside of a loop

First, run the example in for1.py to observe the results:

This loop runs 5 times because the sequence holds 5 numbers (1 through 5). number is the temporary

loop variable that gets assigned each member from the list, one by one, one number for each time the

loop runs

2

Next in for2.py a loop with a sequence of length 3 so that its body statement will run 3 times.

By adding the value of x into our sum variable y times in our loop, our sum variable holds the

product of x and y even though we did not use multiplication

5.1 Part 1 Sample Runs (x as 4, 'python' as lastname)

The value 4 raised to the power 6 is 4096

Listing 1: for1.py

for number in [ 1,2,3,4,5]:

print (number)

5.2 Output of for1.py

1

2

3

4

5

Listing 2: for2.py

#set the value of the variable

# that will hold the sum when the loop is finished

mysum=0

print (' Starting with mysum at value ', mysum, ' nn ')

#set the value of the variable

# to find the product of

x=2

print (' Starting with x at value ', x, ' nn ')

#the number of times this loop runs

# will determine what x is multiplied by

#The sequence is a list containing 1 2 and 3

#The loop variable is number

for number in [1,2,3]:

#add the current value of x into mysum

mysum = mysum + x

3

print (' adding ', x, ' into mysum')

print ()

print ('The sum is ', mysum, ' which should equal ', len ([ 1,2, 3]), ' times ', x)

5.3 Output of for2.py

Starting with mysum at value 0

Starting with x at value 2

adding 2 into mysum

adding 2 into mysum

adding 2 into mysum

The sum is 6 which should equal 3 times 2

6

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

Professional Visual Basic 6 Databases

Authors: Charles Williams

1st Edition

1861002025, 978-1861002020

More Books

Students also viewed these Databases questions

Question

Why is it riskier to buy stocks on margin?

Answered: 1 week ago