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

Kandungan dihapus Kandungan ditambah
Aurora (bincang | sumb.)
Aurora (bincang | sumb.)
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.
 
=== Contoh ===
'''temperature2.py'''
<source lang="python">
<pre>
#menukar Menukar nilai suhu dari fahrenheit atau celsius
 
def print_options():
print "Menu:"
print " 'p' Papar pilihan"
print " 'c' tukar DARIdari celsius"
print " 'f' tukar DARIdari fahrenheit"
print " 'q' keluar program"
 
Baris 159:
print_options()
choice = raw_input("pilihan anda: ")
</presource>
 
Contoh jalanan:
Sample Run:
<pre>
> 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
</pre>
 
> python temperature2.py
area2.py
Menu:
<pre>
'p' Papar pilihan
# By Amos Satterlee
'c' tukar DARIdari celsius
'f' tukar DARIdari 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'''
 
'''area2.py'''
<source lang="python">
# ByOleh Amos Satterlee
print
def hello():
Baris 207 ⟶ 206:
h = input('Height: ')
print 'Width =', w, 'Height =', h, 'so Area =', area(w, h)
</presource>
 
Contoh jalanan:
Sample Run:
<pre>
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
</pre>