Question

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

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

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

If you have any doubts, please give me comment...

from math import *

x = 6

InLen = 3

mysum = 0

#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

#here you can assign length of your lastname

InLen = 4

for number in range(InLen):

#add the current value of x into mysum

mysum = mysum + x

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

print ()

print ('The sum is ', mysum, ' which should equal ', InLen, ' times ', x)

Add a comment
Know the answer?
Add Answer to:
Part 1: Using Idle Write a Python Script that Does the Following 1. At the top...
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
  • 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...

  • Exercise 1 - Create a List to Sum Values Write a python script that sums the...

    Exercise 1 - Create a List to Sum Values Write a python script that sums the values in a list called add_list that contains the following values: (10, 2, -4, 8). Make sure you include a for loop when iterating through the list to sum the values. Take screenshots of the code and output. Exercise 2 – Create a Dictionary Write a python script that prints a dictionary where the key numbers are 1, 2, 3, 4, and 5 and...

  • Select the Python math module function that give you the Euclidean norm, square root of x*x...

    Select the Python math module function that give you the Euclidean norm, square root of x*x + y*y. This is the length of the vector from the origin to point (x, y). sin hypot cos sqrt radians What would be the value printed from the code below? import math print(math.ceil(math.e)) 7 2.718281 3.141592 2 3 What would be the value printed from the code below? import math print(math.floor(math.pi)) 2.718281 3.141592 2 3 6 ex is e raised to the power...

  • Create a new Python program in IDLE, and save it as lab8.py.(Again, I recommend saving lab...

    Create a new Python program in IDLE, and save it as lab8.py.(Again, I recommend saving lab progranms Your Python program will do the following: Create an empty list . Use a for loop to ask the user for 10 numbers. Add each number to your list using the append metha . Use another for loop to print the list in reverse order, one per line . Use a while loop to count how many positive numbers are in the list...

  • Use Python Write a script to perform various basic math and string operations. Use some functions...

    Use Python Write a script to perform various basic math and string operations. Use some functions from Python's math module. Generate formatted output. Basic math and string operations Calculate and print the final value of each variable. a equals 3 to the power of 2.5 b equals 2 b equals b + 3 (use +=) c equals 12 c = c divided by 4 (use /=) d equals the remainder of 5 divided by 3 Built-in functions abs, round, and...

  • Python 1. Open up IDLE and in a multiline comment write “Functions and Sets II,” your...

    Python 1. Open up IDLE and in a multiline comment write “Functions and Sets II,” your name, and the date. 2. Use Python to write a function f(n) that finds the sum of the first n positive integers. Do not use the sum() function or lists. Write the function from scratch. Then use a for loop to print the sums for n = 10, 50, 1000. 3. Finding the Image of a function Suppose that we have a function f...

  • Python Programming assignment : Function of Q3: def isPositive(n): if(n>=0): return True else: return False Write...

    Python Programming assignment : Function of Q3: def isPositive(n): if(n>=0): return True else: return False Write a code for function main, that does the following: -creates a variable and assigns it the value True. - uses a while loop which runs as long as the variable of the previous step is True, to get a number from the user and if passing that number to the function of Q3 results in a True value returned, then add that number to...

  • Python Programming assignment : Function of Q3: def isPositive(n): if(n>=0): return True else: return False Write...

    Python Programming assignment : Function of Q3: def isPositive(n): if(n>=0): return True else: return False Write a code for function main, that does the following: -creates a variable and assigns it the value True. - uses a while loop which runs as long as the variable of the previous step is True, to get a number from the user and if passing that number to the function of Q3 results in a True value returned, then add that number to...

  • USING PYTHON PLEASE Write a program that calculates the factorial value of a number entered by...

    USING PYTHON PLEASE Write a program that calculates the factorial value of a number entered by the user. Remember that x! =x* (x-1)* (x-2)*... *3+ 2* 1. Your program should check the value input by the user to ensure it is valid (i.e., that it is a number > =1). To do this, consider looking at the is digit() function available in Python. If the user enters an incorrect input, your program should continue to ask them to enter a...

  • Create a Python script file called hw12.py. Add your name at the top as a comment,...

    Create a Python script file called hw12.py. Add your name at the top as a comment, along with the class name and date. Ex. 1. a. Texting Shortcuts When people are texting, they use shortcuts for faster typing. Consider the following list of shortcuts: For example, the sentence "see you before class" can be written as "c u b4 class". To encode a text using these shortcuts, we need to perform a replace of the text on the left with...

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