Tutorial Python untuk bukan pengatur cara/Fungsi: Perbezaan antara semakan
Kandungan dihapus Kandungan ditambah
Baris 130:
Sesuatu fungsi membenarkan pemboleh ubah setempat wujud hanya di dalam fungsi itu sahaja. Ia juga akan menyorokkan pemboleh ubah yang mempunyai nama yang sama di luar fungsi tersebut.
'''temperature2.py'''
<source lang="python">
#
def print_options():
print "Menu:"
print " 'p' Papar pilihan"
print " 'c' tukar
print " 'f' tukar
print " 'q' keluar program"
Baris 159:
print_options()
choice = raw_input("pilihan anda: ")
</
Contoh jalanan:
> python temperature2.py▼
Menu:▼
'p' Papar pilihan▼
'c' tukar DARI celsius▼
'f' tukar DARI fahrenheit▼
'q' keluar program▼
pilihan anda: c▼
Suhu dalam Celsius: 30 ▼
Fahrenheit: 86.0▼
pilihan anda: f▼
Suhu dalam Fahrenheit: 60▼
Celsius: 15.5555555556▼
pilihan anda: q▼
▲ > python temperature2.py
area2.py▼
▲ Menu:
# By Amos Satterlee▼
▲ pilihan anda: '''c'''
▲ Fahrenheit: 86.0
▲ pilihan anda: '''f'''
▲ Suhu dalam Fahrenheit: '''60'''
▲ Celsius: 15.5555555556
▲ pilihan anda: '''q'''
▲'''area2.py'''
<source lang="python">
print
def hello():
Baris 207 ⟶ 206:
h = input('Height: ')
print 'Width =', w, 'Height =', h, 'so Area =', area(w, h)
</
Contoh jalanan:
Your Name: Josh▼
Hello!▼
Welcome, Josh▼
▲ Your Name: '''Josh'''
To find the area of a rectangle,▼
▲ Hello!
enter the width and height below.▼
▲ Welcome, Josh
▲ To find the area of a rectangle,
▲ enter the width and height below.
Width: '''-4'''▼
Must be a positive number▼
Width: '''4'''▼
Height: '''3'''▼
Width = 4 Height = 3 so Area = 12▼
== Latihan ==
▲Width: -4
▲Must be a positive number
▲Width: 4
▲Height: 3
▲Width = 4 Height = 3 so Area = 12
|