MENÜ

Honlap címe

Előző - #3 Páratlan és páros | Tartalomjegyzék | Következő - #5 Fizz Buzz

4. GYAKORLAT: TERÜLET ÉS TÉRFOGAT

         terület(10, 4)  →  40
    kerület(10, 4)  →  28
     térfogat(10, 4, 5)  →  200
felület Terület(10, 4, 5)  →  220

A terület, kerület, térfogat és felület egyszerű számítások. Ez a gyakorlat hasonló a 2. gyakorlathoz, „Hőmérséklet-átalakítás” és a 3. gyakorlathoz, „Páratlan és páros”. Ebben a gyakorlatban minden függvény egy egyszerű számítás és returnkijelentés. A terület és a térfogat azonban valamivel bonyolultabb, mivel több paramétert tartalmaz. Ez a gyakorlat továbbra is kihívást jelent a matematikai képletek Python kódra való lefordításában.

Gyakorlat leírása

Ehhez a gyakorlathoz négy függvényt fog írni. A area() and függvényeknek hossza és paraméterei, a függvényeknek pedig , , és magasság paraméterei perimeter()vannak . Ezek a függvények a területet, a kerületet, a térfogatot és a felületet adják vissza.widthvolume()surfaceArea()lengthwidth

A terület, kerület, térfogat és felület kiszámítására szolgáló képletek az alakzat hosszán (L), szélességén (W) és magasságán (H) alapulnak:

  • terület = L × W
  • kerület = L + W + L + W
  • térfogat = H × W × H
  • felület = (H × Szé × 2) + (H × Ma × 2) + (Sz × Ma × 2)

A terület a hossz és a szélesség szorzatából származó kétdimenziós mérés. A kerület a téglalap körüli mind a négy egydimenziós vonal összege. A térfogat egy háromdimenziós mérés a hosszúság, szélesség és magasság szorzatából. A felület a kocka körüli mind a hat kétdimenziós terület összege. Ezen területek mindegyike a három hossz-, szélesség- vagy magasságdimenzió kettőjének szorzatából származik.

A 4-1. ábrán láthatja, hogy ezek a képletek mit mérnek.

Ezek a Python- assertutasítások leállítják a programot, ha feltételük False. Másolja őket a megoldási program aljára. Az Ön megoldása akkor helyes, ha a következő assertállítások feltételei mind igazak :

érvényesítési terület(10, 10) == 100

assert area(0, 9999) == 0

érvényesítési terület(5, 8) == 40

kerület (10, 10) == 40

assert perimeter(0, 9999) == 19998

kerület (5, 8) érvényesítése == 26

assert volume(10, 10, 10) == 1000

assert volume(9999, 0, 9999) == 0

assert volume(5, 8, 10) == 400

assert surfaceArea(10, 10, 10) == 600

érvényesítési felületArea(9999, 0, 9999) == 199960002

assert surfaceArea(5, 8, 10) == 340

Try to write a solution based on the information in this description. If you still have trouble solving this exercise, read the Solution Design and Special Cases and Gotchas sections for additional hints.

Prerequisite concepts: math operators, operator precedence

Solution Design

Use the same strategy you used in Exercise #2, “Temperature Conversion” and translate the math formulas into Python code. Replace the L, W, and H with the length, width, and height parameters. Replace the + addition and × multiplication signs with the + and * Python operators. These functions can be one line long: a return statement that calculates the result and returns it.

Special Cases and Gotchas

Length, width, and height can only be positive numbers. You could write additional code that checks if any of these are negative numbers, and raise an exception in that case. However, this isn’t a requirement for this exercise.

Now try to write a solution based on the information in the previous sections. If you still have trouble solving this exercise, read the Solution Template section for additional hints.

Solution Template

Try to first write a solution from scratch. But if you have difficulty, you can use the following partial program as a starting place. Copy the following code from https://invpy.com/areavolume-template.py and paste it into your code editor. Replace the underscores with code to make a working program:

def area(length, width):

    # Return the product of the length and width:

    return ____ * ____

 

def perimeter(length, width):

    # Return the sum of the length twice and the width twice:

    return ____ * 2 + width * ____

 

def volume(length, width, height):

    # Return the product of the length, width, and height:

    return ____ * ____ * ____

 

def surfaceArea(length, width, height):

    # Return the sum of the area of each of the six sides:

    return ((length * ____) + (length * ____) + (width * ____)) * 2

The complete solution for this exercise is given in Appendix A and https://invpy.com/areavolume.py. You can view each step of this program as it runs under a debugger at https://invpy.com/areavolume-debug/.

Prev - #3 Odd & Even | Table of Contents | Next - #5 Fizz Buzz

 

Asztali nézet