Question

In: Computer Science

dd a new function that is similar to the counting function that you already have -...

  1. dd a new function that is similar to the counting function that you already have - but which uses a for loop to do the exact same thing (count from n//2 to n). Your solution must use a for loop with a range function.
  2. Instrument your code by adding print functions so that we can tell which function is doing the counting, as shown in the sample output below.
  3. Modify your program so that it alternates between using your for loop function and your while loop function each time a number is entered, as shown in the sample output.
  4. Make sure your output exactly matches what is shown below.

Sample session:

C:> python whileloop3.py

Enter a number: 2

Counting with 'while' ... 1 2

total = 2

Enter a number: 2

Counting with 'for' ... 1 2

total = 4

Enter a number: 3

Counting with 'while' ... 1 2 3

total = 7

Enter a number: 3

Counting with 'for' ... 1 2 3

total = 10

Enter a number: 1

Counting with 'while' ... 0 1

total = 11

Enter a number: 1

Counting with 'for' ... 0 1

total = 12

Enter a number: 3

total = 15, exiting

the code:

def look():
    p = int(input("Put a number: "))
    i = p//2-1

    while i < p:
        i = i+1
        print(i ,end=" ")



if __name__ == "__main__":
    look()

Solutions

Expert Solution

Here is code:

def look():

    sum = 0

    choice = 'y'

    while choice == 'y':

        p = int(input("Put a number: "))

        i = p//2

        sum += p

        for it in range(i, p+1):

            print(it, end=" ")

        print("\nTotal = ", sum)

        choice = input("Do you want to continue (n/y) : ")


if __name__ == "__main__":

    look()

Output:


Related Solutions

As you have already seen, Excel does not have a function to calculate the payback period....
As you have already seen, Excel does not have a function to calculate the payback period. We have shown three ways to calculate the payback period, but there are numerous other methods as well. Below, the cash flows for a project are shown. You need to calculate the payback period using two different methods. A. Calculate the payback period in a table. The first three columns of the table will be the year, the cash flow for that year, and...
Your firm is considering producing a new variety of widget. You have already test marketed the...
Your firm is considering producing a new variety of widget. You have already test marketed the widgets at a cost of $90,000. You estimate sales will be $500,000 in the first year, $800,000 in the second year, $500,000 in the third year, and zero after three years. To produce the new widgets will require an investment in working capital at year 0 of $50,000. Net working capital will be 20% of sales in years 1 and 2. Labor, materials, and...
Your firm is considering producing a new variety of widget. You have already test marketed the...
Your firm is considering producing a new variety of widget. You have already test marketed the widgets at a cost of $90,000. You estimate sales will be $500,000 in the first year, $800,000 in the second year, $500,000 in the third year, and zero after three years. To produce the new widgets will require an investment in working capital at year 0 of $50,000. Net working capital will be 20% of sales in years 1 and 2. Labor, materials, and...
Problen 6.70. The marker on your new car indicates that you have already run the first...
Problen 6.70. The marker on your new car indicates that you have already run the first 3,000 miles, at which point the manufacturer recommends a general overhaul. You take your car to a recognized garage and request a review. Upon arrival in the garage there are 8 mechanics available, two of which are new. For a new mechanic, the time it takes to perform the overhaul is a random variable that follows a normal distribution with an expected value of...
Given that I have already trained an SVM regressor with data generated in the sinc function,...
Given that I have already trained an SVM regressor with data generated in the sinc function, sinc(x)=sinπx/πx. What would be the the potential use of the regressor?
Write a new MATLAB function as described in the following: 1. The new function will have...
Write a new MATLAB function as described in the following: 1. The new function will have three input parameters: f, W, C. 2. Parameter f will specify the frequency of the square wave. 3. The parameter W will specify the width of the single pulse as a number of sample periods. 4. The parameter C will specify the number of square wave cycles. 5. Calculate a number of samples, N, to run the simulation for both waveforms. 6. Create one...
The < and == operators for the class Record have already been implemented for you.
The < and == operators for the class Record have already been implemented for you. Write the code necessary to complete the >, <=,>= and != operators. (hint: you do not need to know anything about the Record class to complete)
In terms of having a small company (family owned) --> posted a similar question already with...
In terms of having a small company (family owned) --> posted a similar question already with way more information but no one has answered yet. 1. How should the compliance or lack of compliance with government regulations (such as filing of a W-2 or 1099 for the president of the company) be addressed ????? 2. What if management doesn't comply??? 3. Should legal counsel be hired ? Is a forensic accountant required ???
goal of this function will be to add an element to an already existing array in...
goal of this function will be to add an element to an already existing array in C/C++. bool add(structure dir[], int& size, const char nm[], const char ph[]){ //code here } add = function name structure = structure with two fields { char name[20]; char phone[13]; } int& size = # of entries in dir nm = name that's going to be added ph = phone that's going to be added. return true if entry was successfully added, false if...
You should implement a syntax checking program that is similar to a function of compiler. Compliers...
You should implement a syntax checking program that is similar to a function of compiler. Compliers check programs for syntax errors, but frequently a lack of one symbol (such as a missing brace or com-ment starter) will cause the complier to spill out a hundred lines of diagnostics without identifying the real error. A useful tool in this situation is a program that checks whether everything is balanced. Thus, every right brace, bracket, and parenthesis must correspond to its left...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT