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

Kandungan dihapus Kandungan ditambah
Algazel (bincang | sumb.)
→‎Penyelesaian: Menu sorok OK
Algazel (bincang | sumb.)
→‎Penyelesaian: Kod BM OK
Baris 228:
=== Penyelesaian ===
 
{{penyelesaian|tajuk="Penyelesaian"|teks="
Tulis semula atur cara keluasan2.py dalam Contoh di atas untuk mempunyai fungsi berasingan untuk luas segi empat sama, segi empat tepat dan bulatan (3.14 * jejari**2). 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.
 
<source lang="python">
def squaresegi_empat_sama(lengthpanjang):
return lengthpanjang * lengthpanjang
 
def rectanglesegi_empat_tepat(width lebar, heighttinggi):
return widthlebar * heighttinggi
 
def circlebulatan(radiusjejari):
return 3.14 * radiusjejari ** 2
 
def optionspilihan():
print
print "OptionsPilihan:"
print "s = calculatekira thekeluasan areasegi ofempat a squaresama."
print "c = calculatekira thekeluasan area of a circlebulatan."
print "r = calculatekira thekeluasan areasegi ofempat a rectangletepat."
print "q = quitkeluar"
print
 
print "Program ini akan mengira keluasan segi empat sama, bulatan atau segi empat tepat."
print "This program will calculate the area of a square, circle or rectangle."
choicepilih = "x"
optionspilihan()
while choicepilih != "q":
choicepilih = raw_input("PleaseSila entermasukkan yourpilihan choiceanda: ")
if choicepilih == "s":
lengthpanjang = input("LengthPanjang ofsegi squareempat sama: ")
print "TheKeluasan areasegi ofempat thissama squareini isialah", squaresegi_empat_sama(lengthpanjang)
optionspilihan()
elif choicepilih == "c":
radiusjejari = input("RadiusJejari of the circlebulatan: ")
print "TheKeluasan areabulatan of the circle isialah", circlebulatan(radiusjejari)
optionspilihan()
elif choicepilih == "r":
widthlebar = input("WidthMasukkan oflebar thesegi rectangleempat tepat: ")
heighttinggi = input("HeightTinggi ofsegi theempat rectangletepat: ")
print "TheKeluasan areasegi ofempat thetepat rectangle isialah", rectanglesegi_empat_tepat(widthlebar, heighttinggi)
optionspilihan()
elif choicepilih == "q":
print "",
else:
print "UnrecognizedPilihan optionyang tidak diketahui."
optionspilihan()
</source>