Tutorial Python untuk bukan pengatur cara/Fungsi: Perbezaan antara semakan

Kandungan dihapus Kandungan ditambah
Algazel (bincang | sumb.)
Baris 226:
Tulis semula atur cara ''temperature2.py'' dalam Contoh di atas untuk mempunyai fungsi berasingan untuk luas segi empat sama, segi empat tepat dan bulatan (<code>3.14 * jejari**2</code>). Atur cara ini haruslah memiliki antara muka menu.
 
<!--{{Penyelesaian|tajuk="== Penyelesaian"|teks ===
<!-- Sorok sementara -->
"Tulis semula atur cara ''temperature2.py'' dalam Contoh di atas untuk mempunyai fungsi berasingan untuk luas segi empat sama, segi empat tepat dan bulatan (<code>3.14 * jejari**2</code>). Atur cara ini haruslah memiliki antara muka menu."
}} <!-->
Rewrite the area2.py program from the Examples above to have a separate function for the area of a square, the area of a rectangle, and the area of a circle (<code>3.14 * radius ** 2</code>). This program should include a menu interface.
{{sorok
 
| style = border:1px dashed red; width: 50%;
<source lang="python">
| headerstyle = background: #ccccff; font-size: 110%;
def square(length):
| contentstyle = text-align: center; color:green;
return length * length
| header = Penyelesaian
 
| content = Tulis semula atur cara ''temperature2.py'' dalam Contoh di atas untuk mempunyai fungsi berasingan untuk luas segi empat sama, segi empat tepat dan bulatan (<code>3.14 * jejari**2</code>). Atur cara ini haruslah memiliki antara muka menu
def rectangle(width , height):
}}
return width * height
 
def circle(radius):
return 3.14 * radius ** 2
 
def options():
print
print "Options:"
print "s = calculate the area of a square."
print "c = calculate the area of a circle."
print "r = calculate the area of a rectangle."
print "q = quit"
print
 
print "This program will calculate the area of a square, circle or rectangle."
choice = "x"
options()
while choice != "q":
choice = raw_input("Please enter your choice: ")
if choice == "s":
length = input("Length of square: ")
print "The area of this square is", square(length)
options()
elif choice == "c":
radius = input("Radius of the circle: ")
print "The area of the circle is", circle(radius)
options()
elif choice == "r":
width = input("Width of the rectangle: ")
height = input("Height of the rectangle: ")
print "The area of the rectangle is", rectangle(width, height)
options()
elif choice == "q":
print "",
else:
print "Unrecognized option."
options()
</source>
 
-->
 
{{../Kandungan|Nyahpepijat|Senarai}}