Question

When breaking a problem down, you often encounter elements that you want to use repeatedly in...

  1. When breaking a problem down, you often encounter elements that you want to use repeatedly in your code. Sometimes it's appropriate to write a new function; at other times it's appropriate to write a loop.

What do you think? What kinds of circumstances would lead you to writing a function versus using a loop?

  1. What is the error in the pseudocode below? Re-write the pseudocode in your answer document.

Declare Boolean finished = False

Declare Integer value, cube

While NOT finished

   Display "Enter a value to be cubed."

   Input value;

   Set cube = value^3

   Display value, " cubed is ", cube

End While

  1. Write a Python program for problem #2. Your program should prompt the user for a value to be cubed, print the cube, and continue prompting until they are done. Take a screenshot of your code and output and paste into your answer document.
0 0
Add a comment Improve this question Transcribed image text
Answer #1

def Cube():
finished =False
while(not finished):
n = int(input("Enter value to be cubed:(-1 to stop):"))
if(n==-1):
finished=True
else:
cube = n**3
print(n," cubed is ",cube)
Cube()
  

output:

Enter value to be cubed:(-1 to stop):1
1 cubed is 1
Enter value to be cubed:(-1 to stop):2
2 cubed is 8
Enter value to be cubed:(-1 to stop):3
3 cubed is 27
Enter value to be cubed:(-1 to stop):-1


main.py 1 def Cube(): finished =False while(not finished): 2 3 int(input(Enter value to be cubed:(-1 to stop):)) if (n== -

Add a comment
Know the answer?
Add Answer to:
When breaking a problem down, you often encounter elements that you want to use repeatedly in...
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
  • Write the pseudocode below as a working Python program. Take a screenshot of your code and...

    Write the pseudocode below as a working Python program. Take a screenshot of your code and output and paste into your answer document. User input of ‘y’ or ‘Y’ should cause the loop to continue.    // Constant for the commission rate    // Declare as a global variable    Constant Real COMMISSION_RATE = 0.10     Module main()        // Local variable         Declare String keepGoing = "y"         // Calculate as many commissions         // as needed.         While...

  • Can you send the code and the screenshot of it running? 1) Create a PYHW2 document...

    Can you send the code and the screenshot of it running? 1) Create a PYHW2 document that will contain your algorithms in flowchart and pseudocode form along with your screen shots of the running program. 2) Create the algorithm in both flowchart and pseudocode forms for the following two functions: A. Write a Python function that receives a real number argument representing the sales amount for videos rented so far this month The function asks the user for the number...

  • You recently graduated college and you are applying for a programming job that requires the understanding...

    You recently graduated college and you are applying for a programming job that requires the understanding of loops in Python. The manager you are interviewing with has asked you to take an assessment to prove your programming knowledge. Below are the requirements for the programming skills test. In Python, create a program that meets the following requirements: Take two integers from the user. Save the lower number as x. Save the largest integer as y. Write a loop that counts...

  • You recently graduated college and you are applying for a programming job that requires the understanding...

    You recently graduated college and you are applying for a programming job that requires the understanding of loops in Python. The manager you are interviewing with has asked you to take an assessment to prove your programming knowledge. Below are the requirements for the programming skills test. In Python, create a program that meets the following requirements: Take two integers from the user. Save the lower number as x. Save the largest integer as y. Write a loop that counts...

  • Write a Python Program that displays repeatedly a menu as shown in the sample run below....

    Write a Python Program that displays repeatedly a menu as shown in the sample run below. The user will enter 1, 2, 3, 4 or 5 for choosing addition, substation, multiplication, division or exit respectively. Then the user will be asked to enter the two numbers and the result for the operation selected will be displayed. After an operation is finished and output is displayed the menu is redisplayed, you may choose another operation or enter 5 to exit the...

  • use python to write You want to figure out how many days you spent in your...

    use python to write You want to figure out how many days you spent in your highschool education. Assign the start and the end year for your highschool studies (or you can make up two years that are at least 2-4 years apart). For simplicity, we will not worry about the months or the actual dates. Write a program that will display how many days you had in that range (inclusive of both the years). For example, if you started...

  • Lab Topics • The basics of Array object Use the following Coding Guidelines • When declaring...

    Lab Topics • The basics of Array object Use the following Coding Guidelines • When declaring a variable, you usually want to initialize it. Remember you cannot initialize a number with a string. Remember variable names are case sensitive. Use tabs or spaces to indent code within blocks (code surrounded by braces). Use white space to make your program more readable. Use comments after the ending brace of classes, methods, and blocks to identify to which block it belongs. Problem...

  • Problem: Write a short C++ program that gets the side of a cube and the radius...

    Problem: Write a short C++ program that gets the side of a cube and the radius of a sphere from the keyboard and writes to a file the surfaces of these shapes.             ------------------------------------------------------------------------------------------------------------------------ Your task: implement in C++ the algorithm solution shown below. ------------------------------------------------------------------------------------------------------------------------ Part A (79 points) Algorithm solution (in pseudocode): To accomplish this task, your program will have to take the following steps: 1. Declare a variable named outFile that represents an output stream. 2. Declare a...

  • DQuestion 19 28 pts Problem 2. Quadratic Equations A quadratic equation has the form: a bc0 The two solutions are given by the formula: 2a Write a program with a loop that a) solves quadratic equatio...

    DQuestion 19 28 pts Problem 2. Quadratic Equations A quadratic equation has the form: a bc0 The two solutions are given by the formula: 2a Write a program with a loop that a) solves quadratic equations with coefficients read from the terminal, b) visualizes the corresponding quadratic function az2 brc0using the matplotlib module. Instructions Write all code for this problem in a file p2.py 1. The program consists of a main whtle loop (an infinite loop) in which the user...

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