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

Kandungan dihapus Kandungan ditambah
Aurora (bincang | sumb.)
Aurora (bincang | sumb.)
Baris 103:
Atur cara ini akan menjadi lebih kompleks untuk 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?''
: Memasukkan nombor 5 ke dalam pemboleh ubah ''<code>number''</code>.
 
; ''Apakah baris seterusnya?''
: Baris seterusnya ialah: <code>while number > 1:</code>
 
; ''Apa yang ia buat?''
: 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.
 
; ''Apa yang sepatutnya ia buat sekarang?''
: Jika <code>number > 1</code> benar, maka dua baris seterusnya akan berjalan.
 
; ''Jadi adakah <code>number > 1</code>?''
: Nilai terakhir dimasukkan kepada <code>number</code> ialah 5 dan 5 > 1 jadi ya.
 
; ''Jadi, apa ygapakah baris seterusnya?''
: Since theMemandangkan <code>while</code> was true thebenar, nextbaris lineseterusnya isialah: <code>print ".",</code>
; WhatApa doesyang thatbaris lineitu dobuat?
: Mencetak satu titik dan oleh sebab penyataan itu berakhir dengan ',' penyataan <code>print</code> seterusnya tidak akan berada di baris skrin berbeza.
: Prints one dot and since the statement ends with a , the next print statement will not be on a different screen line.
; Apakah baris seterusnya?
; What is the next line?
: <code>number = number - 1</code> sincekerana thatia isbaris followingseterusnya linedan andtiada thereperubahan aredalam no indent changesengsotan.
; WhatApa doesyang itia dobuat?
: ItIa calculatesmengira <code>number - 1</code>, whichiaitu isnilai the current value ofterkini <code>number</code> (oratau 5) subtractsditolak dengan 1 from it, anddan makesmenjadikan thatnilai thetersebut newnilau value ofbaru <code>number</code>. SoJadi, basicallypada itasasnya changesia number'smenukai valuenilai <code>number</code> fromdaripada 5 tokepada 4.
; Apakah baris seterusnya?
; What is the next line?
: Well,Tahap theengsotan indentberubah, leveljadi decreaseskita soperlu welihat haveapakah tojenis lookstruktur atkawalan what type of control structure it istersebut. ItIa ismerupakan agelung ''while loop'', sojadi wekita haveperlu tokembali goke back to theklausa <code>while</code> clause which isiaitu <code>while number > 1:</code>
; WhatApa doesyang itia dobuat?
: ItIa looksmelihat atkepada the value ofnilai <code>number</code>, which isiaitu 4, anddan comparesmembandingkannya it todengan 1 anddan sincedisebabkan <code>4 > 1</code>, thegelung ''while loopterus continuesberjalan.
; Apakah baris seterusnya?
; What is the next line?
: SinceOleh thekerana gelung ''while'' loopmasih was truebenar, the nextbaris lineseterusnya isialah: <code>print ".",</code>
; WhatApa doesyang itia dobuat?
: Ia mencetak titik kedua pada baris tersebut.
: It prints a second dot on the line.
; Apakah baris seterusnya?
; What is the next line?
: NoTiada indentperbezaan changeengsotan, so it isjadi: <code>number = number - 1</code>
; AndDan whatapa doesyang itia dobuat?
: ItIa takesmengambil thenilai current value ofsemasa <code>number</code> (4), subtractstolak 1 from itdaripadanya, whichmemberikan gives itnilai 3 andyang then finally makes 3kemudian thedisimpan newsebagai valuenilai ofterbaru <code>number</code>.
; Apakah baris seterusnya?
; What is the next line?
: SinceMemandangkan thereterdapat isperubahan anengsotan indentyang changedisebabkan causedoleh by the end of thegelung ''while'' loopberakhir, thebaris nextseterusnya line isialah: <code>while number > 1:</code>
; WhatApa doesyang itia dobuat?
: ItIa comparesmembandingkan thenilai current value ofsemasa <code>number</code> (3) todengan 1. <code>3 > 1</code> sojadi thegelung ''while'' loopakan continuesditeruskan.
; Apakah baris seterusnya?
; What is the next line?
: SinceDisebabkan thesyarat gelung ''while'' loopmasih condition was true thebenar, nextbaris lineseterusnya isialah: <code>print ".",</code>
; AndDan itapa doesyang whatia buat?
: Titik ketiga dicetak di baris tersebut.
: A third dot is printed on the line.
; Apakah baris seterusnya?
; What is the next line?
: It is: <code>number = number - 1</code>
; WhatApa doesyang itia dobuat?
: ItIa takesmengambil thenilai current value ofsemasa <code>number</code> (3), subtracts from ittolak 1 anddaripadanya makesdan themenyimpan 2 the newsebagai valuenilai ofbaru <code>number</code>.
; Apakah baris seterusnya?
; What is the next line?
: BackKempali upke topermulaan the start of thegelung ''while loop'': <code>while number > 1:</code>
; WhatApa doesyang itia dobuat?
: ItIa comparesmembandingkan thenilai current value ofsemasa <code>number</code> (2) todengan 1. Since Oleh sebab <code>2 > 1</code> thegelung ''while'' loopakan continuesberterusan.
; Apakah baris seterusnya?
; What is the next line?
: SinceOleh thekerana gelung ''while'' loop is continuingditeruskan: <code>print ".",</code>
; WhatApa doesyang itia dobuat?
: 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.
; Apakah baris seterusnya?
; What is the next line?
: It's: <code>number = number - 1</code>
; WhatApa doesyang itia dobuat?
: Takes the current value of number (2) subtracts 1 and makes 1 the new value of number.
; Apakah baris seterusnya?
; What is the next line?
: Back up to the while loop: <code>while number > 1:</code>
; WhatApa doesyang thebaris lineitu dobuat?
: It compares the current value of number (1) to 1. Since <code>1 > 1</code> is false (one is not greater than one), the while loop exits.
; Apakah baris seterusnya?
; What is the next line?
: Since the while loop condition was false the next line is the line after the while loop exits, or: <code>print</code>
; WhatApa doesyang thatbaris lineitu dobuat?
: Makes the screen go to the next line.
; Why doesn't the program print 5 dots?
Baris 178:
: 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: <code>while number > 0:</code> Another way would be to change the conditional to: <code>number >= 1</code> There are a couple others.