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

Kandungan dihapus Kandungan ditambah
Aurora (bincang | sumb.)
Aurora (bincang | sumb.)
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.
 
ProgramAtur cara seterusnya kitayang akan menghasilkankita satu codeteliti ialah untuk suatu programatur cara yang sepatutnya akan memaparkan 5 dot-dottitik pada skrin. Bagaimanapun, ini ialah apa yang programatur cara outputkan:-
 
<pre> . . . . </pre>
 
Dan ini adalah code nyakodnya.
<source lang="python">
<pre>
number = 5
while number > 1:
Baris 107 ⟶ 99:
number = number - 1
print
</presource>
 
ProgramAtur cara ini akan menjadi lebih kompleks apabilauntuk diteliti kerana ia mempunyai struktur kawalan (bahagian berengsot). Mari kita mulakan:-
 
; ''Apakah baris pertama yang akan dijalankan?''
: Baris pertama fail: <code>number = 5</code>
 
'; ''Apa yang ia buat?'''<br>
Program akan menjadi lebih kompleks apabila ia mempunyai struktur kawalan. Mari kita mulakan:-
Masukkan: numberMemasukkan nombor 5 dike dalam pemboleh ubah (variable) ''number''.<br>
 
'; ''Apakah yang baris pertama akan jalankanseterusnya? ?'''<br>
: Baris pertamaseterusnya ialah: <code>while number => 51:<br/code>
 
'; ''Apa yang ia buat??'''<br>
: 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>
 
'; ''Apa yang sepatutnya ia buat sekarang?'''<br>
'''Apakah baris seterusnya?'''<br>
If: Jika <code>number > 1</code> adalahbenar, true kemudianmaka dua baris seterusnya akan berjalan.<br>
Baris seterusnya ialah: while number > 1:<br>
 
; ''Jadi adakah <code>number > 1</code>?''
'''Apa yang ia buat?'''<br>
: Nilai terakhir dimasukkan nomborkepada <code>number</code> ialah 5 anddan 5 > 1 sojadi yesya.<br>
Penyataan while secara amnya menunjukkan pada ekspresi, and if it is true they do the next indented block of code, otherwise they skip the next indented block of code.<br>
 
; '''ApakahJadi, apa yg baris seterusnya?'''<br>
'''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?
'''So is number > 1?'''<br>
: Prints one dot and since the statement ends with a , the next print statement will not be on a different screen line.
Nilai terakhir dimasukkan nombor ialah 5 and 5 > 1 so yes.<br>
; What is the next line?
'''Jadi, apa yg baris seterusnya?'''
: number = number - 1 since that is following line and there are no indent changes.
Since the while was true the next line is: print ".",
; What does that lineit do?
: 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?
: 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:
number = number - 1 since that is following line and there are no indent changes.
; What does it do?
: It calculateslooks number - 1, which isat the current value of number, (orwhich 5)is subtracts 1 from it4, and makescompares thatit theto new1 valueand ofsince number.4 So> basically1 itthe changeswhile number'sloop value from 5 to 4continues.
; What is the next line?
: Since the while loop was true, the next line is: print ".",
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?
: It prints a second dot on the line.
It looks at the value of number, which is 4, and compares it to 1 and since 4 > 1 the while loop continues.
; 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 ".",
What; And what does it do?
: 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 Noan indent change socaused itby the end of the while loop, the next line is: number =while number -> 1:
And; whatWhat does it do?
: It takescompares the current value of number (43), subtractsto 1 from it, which gives it. 3 and> then1 finally makes 3so the newwhile valueloop of numbercontinues.
; What is the next line?
: Since there is an indent change caused by the endwhile ofloop thecondition whilewas loop,true the next line is: whileprint number > 1:".",
What; doesAnd it dodoes what?
: A third dot is printed on the 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
Since the while loop condition was true the next line is: print ".",
And; itWhat does whatit do?
: 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 Itthe is:start numberof =the while loop: while number -> 1:
; What does it do?
: It takescompares the current value of number (32) subtracts from itto 1. andSince makes2 the> 21 the newwhile valueloop of numbercontinues.
; What is the next line?
: Back up to the start ofSince the while loop: whileis numbercontinuing: >print 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.
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
Since the while loop is continuing: print ".",
; What does it do?
: It comparesTakes the current value of number (32) tosubtracts 1. 3and >makes 1 so the whilenew loopvalue continuesof number.
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 It's:the numberwhile =loop: while number -> 1:
; What does itthe line do?
: It Takescompares the current value of number (21) subtractsto 1. andSince makes1 > 1 theis newfalse value(one ofis numbernot greater than one), the while loop exits.
; What is the next line?
: Since the while Backloop upcondition towas false the whilenext loop:line is the line after the while numberloop >exits, 1or: print
; What does thethat line do?
: Makes the screen go to the next line.
It compares the current value of number (1) to 1. Since 1 > 1 is false (one is not greater than one), the while loop exits.
; Why doesn't the program print 5 dots?
What is the next line?
: The loop exits 1 dot too soon.
Since the while loop condition was false the next line is the line after the while loop exits, or: print
; How can we fix that?
What does that line do?
: MakesMake the screenloop goexit to the1 nextdot linelater.
; And how do we do that?
Why doesn't the program print 5 dots?
: 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.
The loop exits 1 dot too soon.
How can we fix that?
Make the loop exit 1 dot later.
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.