Tutorial Python untuk bukan pengatur cara/Nyahpepijat: Perbezaan antara semakan
Kandungan dihapus Kandungan ditambah
Baris 65:
; ''Soalan:'' Apakah baris pertama yang dijalankan Python?
: ''Jawapan:'' Baris pertama sentiasa dijalankan dahulu. Di dalam kes ini: <code>height = input("Height: ")</code>
; ''Soalan:'' Apa yang baris itu buat?
: ''Jawapan:'' Cetak <code> Height: </code>, menunggu pengguna untuk memasukkan nombor, dan memasukkannya ke dalam pemboleh ubah "height".
; ''Soalan:'' Apakah baris yang dijalankan seterusnya?
: ''Jawapan:'' Secara umumnya, ia adalah baris seterusnya yang merupakan: <code>width = input("Width: ")</code>
; ''Soalan:'' Apa yang baris itu buat?
: ''Jawapan:'' Cetak <code>Width: </code>, menunggu pengguna untuk memasukkan nombor, dan memasukkannya ke dalam pemboleh ubah "width".
; ''Soalan:'' Apakah baris yang dijalankan seterusnya?
: ''Jawapan:'' Jika baris seterusnya tidak diengsot lebih atau kurang daripada baris yang sedia ada, ia adalah baris yang akan dijalankan, jadi: <code>print "perimeter = ", width + height + width + width</code> (Ia boleh juga menjalankan fungsi dalam baris semasa, tetapi itu dari bab akan datang.)
; ''Soalan:'' Apa yang baris itu buat?
: ''Jawapan:'' Pertama, ia mencetak <code>perimeter = </code>, kemudian ia mencetak nilai <code>width + height + width + width</code>.
; ''Soalan:'' Adakah <code>width + height + width + width</code> mengira perimeter dengan betul?
: ''Jawapan:'' Mari lihat, perimeter segi empat ialah bawah (width) campur sisi kiri (height) campur yang atas (width) campur sisi kanan (hah?). Hal terakhir seharusnya panjang sebelah kanan atau ketinggiannya.
; ''Soalan:'' Adakah anda fahami mengapa sesetengah kiraan perimeter dikira dengan "betul"?
: ''Jawapan:'' Ia dikira dengan betul apabila lebar dan tinggi adalah sama.
Dan ini adalah
<source lang="python">
number = 5
while number > 1:
Baris 107 ⟶ 99:
number = number - 1
print
</
; ''Apakah baris pertama yang akan dijalankan?''
: Baris pertama fail: <code>number = 5</code>
▲Program akan menjadi lebih kompleks apabila ia mempunyai struktur kawalan. Mari kita mulakan:-
: Baris
: Penyataan <code>while</code> secara amnya melihat pada ungkapannya, dan jika ia benar komputer akan menjalankan blok kod berengsot seterusnya, jika tidak blok kod berengsot tersebut akan dilangkau.
▲Masukkan number 5 di dalam pemboleh ubah (variable) number.<br>
'''Apakah baris seterusnya?'''<br>▼
; ''Jadi adakah <code>number > 1</code>?''
▲'''Apa yang ia buat?'''<br>
: Nilai terakhir dimasukkan
▲'''Apa yang sepatutnya ia buat sekarang?'''<br>
: Since the while was true the next line is: print ".",▼
▲If number > 1 adalah true kemudian dua baris seterusnya akan berjalan.<br>
; What does that line do?▼
▲Nilai terakhir dimasukkan nombor ialah 5 and 5 > 1 so yes.<br>
; What is the next line?▼
▲Since the while was true the next line is: print ".",
; What does
: It calculates number - 1, which is the current value of number (or 5) subtracts 1 from it, and makes that the new value of number. So basically it changes number's value from 5 to 4.
▲ Prints one dot and since the statement ends with a , the next print statement will not be on a different screen line.
; What is the next line?
▲ number = number - 1 since that is following line and there are no indent changes.
; What does it do?
; What is the next line?
▲ Well, the indent level decreases so we have to look at what type of control structure it is. It is a while loop, so we have to go back to the while clause which is while number > 1:
; What does it do?
; What is the next line?
: No indent change so it is: number = number - 1
▲ Since the while loop was true, the next line is: print ".",
: It takes the current value of number (4), subtracts 1 from it, which gives it 3 and then finally makes 3 the new value of number.
▲ It prints a second dot on the line.
; What is the next line?
: Since there is
; What is the next line?
It compares the current value of number (3) to 1. 3 > 1 so the while loop continues.▼
; What is the next line?
: It is: number = number - 1
: It takes the current value of number (3) subtracts from it 1 and makes the 2 the new value of number.
▲ A third dot is printed on the line.
; What is the next line?
: Back up to
; What does it do?
; What is the next line?
:
; What does it do?
▲ It compares the current value of number (2) to 1. Since 2 > 1 the while loop continues.
; What is the next line?
: It's: number = number - 1
; What does it do?
▲:
▲ It discovers the meaning of life, the universe and everything. I'm joking. (I had to make sure you were awake.) The line prints a fourth dot on the screen.
; What is the next line?
: Back up to
; What does
: It
; What is the next line?
: Since the while
; What does
: Makes the screen go to the next line.
; Why doesn't the program print 5 dots?▼
▲What is the next line?
; How can we fix that?▼
▲What does that line do?
:
; And how do we do that?▼
▲Why doesn't the program print 5 dots?
▲ The loop exits 1 dot too soon.
▲How can we fix that?
▲And how do we do that?
▲ There are several ways. One way would be to change the while loop to: while number > 0: Another way would be to change the conditional to: number >= 1 There are a couple others.
|