Search
Search titles only
By:
Search titles only
By:
Log in
Register
Search
Search titles only
By:
Search titles only
By:
Menu
Install the app
Install
Forums
New posts
All threads
Latest threads
New posts
Trending threads
Trending
Search forums
What's new
New posts
New ads
New profile posts
Latest activity
Free Ads
Latest reviews
Search ads
Members
Current visitors
New profile posts
Search profile posts
Contact us
Latest ads
Power Lifting Lever Belt
SkullVamp
Updated:
Saturday at 10:32 PM
Ad icon
port.lk Domain for sale
Lankan-Tech
Updated:
Saturday at 3:55 PM
Colombo
Kaduwela - Two Storey House for Sale
dilrasan
Updated:
Thursday at 2:23 PM
Ad icon
Wechat qr verification
Pawan2005
Updated:
Thursday at 1:28 AM
🚀 GOOGLE AI PRO 18 MONTHS ACTIVATION 🚀
sayuru bandara
Updated:
Wednesday at 5:34 PM
Electronics
Vehicles
Property
Search
Reply to thread
Forums
General
Education
Python The Worlds Most flexible and easist Language..
Get the App
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Message
<blockquote data-quote="gayan kalhara" data-source="post: 3322065" data-attributes="member: 60168"><p><strong>part 4 Count to 10</strong></p><p></p><p style="text-align: center"><strong>4.1 While loops</strong></p> <p style="text-align: center"></p> <p style="text-align: center"></p><p>Presenting our first control structure. Ordinarily the computer starts with the first line and then goes down from there. Control structures change the order that statements are executed or decide if a certain statement will be run. Here’s the</p><p>source for a program that uses the while control structure:</p><p></p><p>a = 0</p><p>while a < 10:</p><p>a = a + 1</p><p>print a</p><p></p><p>And here is the extremely exciting output:</p><p>1</p><p>2</p><p>3</p><p>4</p><p>5</p><p>6</p><p>7</p><p>8</p><p>9</p><p>10</p><p></p><p>(And you thought it couldn’t get any worse after turning your computer into a five dollar calculator?) So what does</p><p>the program do? First it sees the line a = 0 and makes a zero. Then it sees while a < 10: and so the computer</p><p>checks to see if a < 10. The first time the computer sees this statement a is zero so it is less than 10. In other words</p><p>while a is less than ten the computer will run the tabbed in statements.</p><p>Here is another example of the use of while:</p><p></p><p>13</p><p>a = 1</p><p>s = 0</p><p>print ’Enter Numbers to add to the sum.’</p><p>print ’Enter 0 to quit.’</p><p>while a != 0 :</p><p>print ’Current Sum:’,s</p><p>a = input(’Number? ’)</p><p>s = s + a</p><p>print ’Total Sum =’,s</p><p>The first time I ran this program Python printed out:</p><p>File "sum.py", line 3</p><p>while a != 0</p><p>ˆ</p><p>SyntaxError: invalid syntax</p><p>I had forgotten to put the : after the while. The error message complained about that problem and pointed out where</p><p>it thought the problem was with the ˆ . After the problem was fixed here was what I did with the program:</p><p></p><p>Enter Numbers to add to the sum.</p><p>Enter 0 to quit.</p><p>Current Sum: 0</p><p>Number? 200</p><p>Current Sum: 200</p><p>Number? -15.25</p><p>Current Sum: 184.75</p><p>Number? -151.85</p><p>Current Sum: 32.9</p><p>Number? 10.00</p><p>Current Sum: 42.9</p><p>Number? 0</p><p>Total Sum = 42.9</p><p></p><p>Notice how print ’Total Sum =’,s is only run at the end. The while statement only affects the line that are tabbed in (a.k.a. indented). The != means does not equal so while a != 0 : means until a is zero run the tabbed in statements that are afterwards. Now that we have while loops, it is possible to have programs that run forever. An easy way to do this is to write a program like this:</p><p></p><p>while 1 == 1:</p><p>print "Help, I’m stuck in a loop."</p><p></p><p>This program will output Help, I’m stuck in a loop. until the heat death of the universe or you stop it. </p><p></p><p><em>Tip</em> </p><p><strong>The way to stop it is to hit the Control (or Ctrl) button and ‘c’ (the letter) at the same time. This will kill the program. (Note: sometimes you will have to hit enter after the Control C.)</strong></p></blockquote><p></p>
[QUOTE="gayan kalhara, post: 3322065, member: 60168"] [b]part 4 Count to 10[/b] [CENTER][B]4.1 While loops[/B] [/CENTER] Presenting our first control structure. Ordinarily the computer starts with the first line and then goes down from there. Control structures change the order that statements are executed or decide if a certain statement will be run. Here’s the source for a program that uses the while control structure: a = 0 while a < 10: a = a + 1 print a And here is the extremely exciting output: 1 2 3 4 5 6 7 8 9 10 (And you thought it couldn’t get any worse after turning your computer into a five dollar calculator?) So what does the program do? First it sees the line a = 0 and makes a zero. Then it sees while a < 10: and so the computer checks to see if a < 10. The first time the computer sees this statement a is zero so it is less than 10. In other words while a is less than ten the computer will run the tabbed in statements. Here is another example of the use of while: 13 a = 1 s = 0 print ’Enter Numbers to add to the sum.’ print ’Enter 0 to quit.’ while a != 0 : print ’Current Sum:’,s a = input(’Number? ’) s = s + a print ’Total Sum =’,s The first time I ran this program Python printed out: File "sum.py", line 3 while a != 0 ˆ SyntaxError: invalid syntax I had forgotten to put the : after the while. The error message complained about that problem and pointed out where it thought the problem was with the ˆ . After the problem was fixed here was what I did with the program: Enter Numbers to add to the sum. Enter 0 to quit. Current Sum: 0 Number? 200 Current Sum: 200 Number? -15.25 Current Sum: 184.75 Number? -151.85 Current Sum: 32.9 Number? 10.00 Current Sum: 42.9 Number? 0 Total Sum = 42.9 Notice how print ’Total Sum =’,s is only run at the end. The while statement only affects the line that are tabbed in (a.k.a. indented). The != means does not equal so while a != 0 : means until a is zero run the tabbed in statements that are afterwards. Now that we have while loops, it is possible to have programs that run forever. An easy way to do this is to write a program like this: while 1 == 1: print "Help, I’m stuck in a loop." This program will output Help, I’m stuck in a loop. until the heat death of the universe or you stop it. [I]Tip[/I] [B]The way to stop it is to hit the Control (or Ctrl) button and ‘c’ (the letter) at the same time. This will kill the program. (Note: sometimes you will have to hit enter after the Control C.)[/B] [/QUOTE]
Insert quotes…
Verification
Hata thunen beduwama keeyada? (60 bedeema thuna)
Post reply
Top
Bottom