Python Program On Miles Per Gallon Loop

a few lines of code using Boolean operators this way. If Just make sure that your code doesn't get so tricky that

1. Compare and contrast the following pairs of terms.

(a) Definite loop vs. Indefinite loop

(b) For loop vs. While loop

(c) Interactive loop vs. Sentinel loop

(d) Sentinel loop vs. End-of-file loop

2. Give a truth table that shows the (Boolean) value of each of the following Boolean expressions, for every possible combination of "input" values. Hint: including columns for "intermediate" expressions is helpful.

3. Write a while loop fragment that calculates the following values.

(a) Sum of the first n countingnumbers: 1 2 3 n

(c) Sum of a series of numbers entered by the user until the value 999 is entered. Note: 999 should not be part of the sum.

(d) The number of times a whole number n can be divided by 2 (using integer division) before reaching 1 (i.e., log2n).

4. The Fibonacci sequence starts 1 1 2 3 5 8 . Each number in the sequence (after the first two) is the sum of the previous two. Write a program that computes and outputs the nth Fibonacci number, where n is a value entered by the user.

5. The National Weather Service computes the windchill index using the following formula.

Where T is the temperature in degrees Fahrenheit, and V is the wind speed in miles per hour.

Write a program that prints a nicely formatted table of windchill values. Rows should represent wind speed for 0 to 50 in 5 mph increments, and the columns represent temperatures from -20 to +60 in 10-degree increments.

6. Write a program that uses a while loop to determine how long it takes for an investment to double at a given interest rate. The input will be an annualized interest rate, and the output is the number of years it takes an investment to double. Note: the amount of the initial investment does not matter; you can use $1.

7. The Syracuse (also called Collatz or Hailstone) sequence is generated by starting with a natural number and repeatedly applying the following function until reaching 1.

For example, the Syracuse sequence starting with 5 is: 5,168421- Itisan open question in mathematics whether this sequence will always go to 1 for every possible starting value.

Write a program that gets a starting value from the user and then prints the Syracuse sequence for that starting value.

x/ 2 ifxiseven 3x+ 1 if x is odd x/ 2 ifxiseven 3x+ 1 if x is odd

8. A postive whole number n > 2 is prime if no number between 2 and ^Jli (inclusive) evenly divides n. Write a program that accepts a value of n as input and determines if the value is prime. If n is not prime, your program should quit as soon as it finds a value that evenly divides n.

9. Modify the previous program to find every prime number less than or equal to n.

10. The greatest common divisor (GCD) of two values can be computed using Euclid's algorithm. Starting with the values m and n, we repeatedly apply the formula: n, m = m, n%m until m is 0. At that point, n is the GCD of the original m and n. Write a program that finds the GCD of two numbers using this algorithm.

11. Write a program that computes the fuel efficiency of a multi-leg journey. The program will first prompt for the starting odometer reading and then get information about a series of legs. For each leg, the user enters the current odometer reading and the amount of gas used (separated by a space). The user signals the end of the trip with a blank line. The program should print out the miles per gallon achieved on each leg and the total MPG for the trip.

12. Modify the previous program to get its input from a file.

13. Heating and cooling degree-days are measures used by utility companies to estimate energy requirements. If the average temperature for a day is below 60, then the number of degrees below 60 is added to the heating degree-days. If the temperature is above 80, the amount over 80 is added to the cooling degree-days. Write a program that accepts a sequence of average daily temps and computes the running total of cooling and heating degree-days. The program should print these two totals after all the data has been processed.

14. Modify the previous program to get its input from a file.

15. Write a program that graphically plots a regression line, that is, the line with the best fit through a collection of points. First ask the user to specify the data points by clicking on them in a graphics window. To find the end of input, place a small rectangle labelled "Done" in the lower left corner of the window; the program will stop gathering points when the user clicks inside that rectangle.

The regression line is the line with the following equation:

where

I x - n x x is the mean of the x-values and y is the mean of the y-values.

As the user clicks on points, the program should draw them in the graphics window and keep track of the count of input values and the running sum of x, y, x2 and xy values. When the user clicks inside the "Done" rectangle, the program then computes value of y (using the equations above) correponding to the x values at the left and right edges of the window to compute the endpoints of the regression line spanning the window. After the line is drawn, the program will pause for another mouse click before closing the window and quitting.

0 0

Responses

  • TOBIAS
    How to end loop in sentinel looping python?
    6 years ago

Post a comment