⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 node6.html

📁 Pythone Library reference. it is ok and simple.
💻 HTML
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<!-- saved from url=(0061)http://www.honors.montana.edu/~jjc/easytut/easytut/node6.html -->
<!--Converted with LaTeX2HTML 99.2beta6 (1.42)original version by:  Nikos Drakos, CBLU, University of Leeds* revised and updated by:  Marcus Hennecke, Ross Moore, Herb Swan* with significant contributions from:  Jens Lippmann, Marek Rouchal, Martin Wilck and others --><HTML><HEAD><TITLE>Count to 10</TITLE>
<META content="Count to 10" name=description>
<META content=easytut name=keywords>
<META content=document name=resource-type>
<META content=global name=distribution>
<META content="text/html; charset=iso-8859-1" http-equiv=Content-Type>
<META content="MSHTML 5.00.2614.3500" name=GENERATOR>
<META content=text/css http-equiv=Content-Style-Type><LINK 
href="node6_files/easytut.css" rel=STYLESHEET><LINK href="node7.html" 
rel=next><LINK href="node5.html" rel=previous><LINK href="easytut.html" 
rel=up><LINK href="node7.html" rel=next></HEAD>
<BODY><!--Navigation Panel--><A 
href="http://www.honors.montana.edu/~jjc/easytut/easytut/node7.html" 
name=tex2html214><IMG align=bottom alt=next border=0 height=24 
src="node6_files/next.png" width=37></A> <A 
href="http://www.honors.montana.edu/~jjc/easytut/easytut/easytut.html" 
name=tex2html210><IMG align=bottom alt=up border=0 height=24 
src="node6_files/up.png" width=26></A> <A 
href="http://www.honors.montana.edu/~jjc/easytut/easytut/node5.html" 
name=tex2html204><IMG align=bottom alt=previous border=0 height=24 
src="node6_files/prev.png" width=63></A> <A 
href="http://www.honors.montana.edu/~jjc/easytut/easytut/node2.html" 
name=tex2html212><IMG align=bottom alt=contents border=0 height=24 
src="node6_files/contents.png" width=65></A> <BR><B>Next:</B> <A 
href="http://www.honors.montana.edu/~jjc/easytut/easytut/node7.html" 
name=tex2html215>Decisions</A> <B>Up:</B> <A 
href="http://www.honors.montana.edu/~jjc/easytut/easytut/easytut.html" 
name=tex2html211>Non-Programmers Tutorial For Python</A> <B>Previous:</B> <A 
href="http://www.honors.montana.edu/~jjc/easytut/easytut/node5.html" 
name=tex2html205>Who Goes There?</A> &nbsp; <B><A 
href="http://www.honors.montana.edu/~jjc/easytut/easytut/node2.html" 
name=tex2html213>Contents</A></B> <BR><BR><!--End of Navigation Panel--><!--Table of Child-Links--><A 
name=CHILD_LINKS><STRONG>Subsections</STRONG></A> 
<UL>
  <LI><A 
  href="http://www.honors.montana.edu/~jjc/easytut/easytut/node6.html#SECTION00610000000000000000" 
  name=tex2html216>While loops</A> 
  <LI><A 
  href="http://www.honors.montana.edu/~jjc/easytut/easytut/node6.html#SECTION00620000000000000000" 
  name=tex2html217>Examples</A> </LI></UL><!--End of Table of Child-Links-->
<HR>

<H1><A name=SECTION00600000000000000000>Count to 10</A> </H1>
<P>
<H1><A name=SECTION00610000000000000000>While loops</A> </H1>
<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 source for a program that uses the while control structure: <PRE>a = 0
while a &lt; 10:
        a = a + 1
        print a
</PRE>
<P>And here is the extremely exciting output: <PRE>1
2
3
4
5
6
7
8
9
10
</PRE>
<P>(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 
<TT>a = 0</TT> and makes a zero. Then it sees <TT>while a &lt; 10:</TT> and so 
the computer checks to see if <TT>a &lt; 10</TT>. 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. 
<P>Here is another example of the use of <TT>while</TT>: <PRE>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
</PRE>
<P>The first time I ran this program Python printed out: <PRE>  File "sum.py", line 3
    while a != 0 
                ^
SyntaxError: invalid syntax
</PRE>I had forgotten to put the <TT>:</TT> after the while. The error message 
complained about that problem and pointed out where it thought the problem was 
with the <CODE>SPMquot</CODE>^" . After the problem was fixed here was what I 
did with the program: <PRE>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
</PRE>
<P>Notice how <TT>print 'Total Sum =',s</TT> is only run at the end. The 
<TT>while</TT> statement only affects the line that are tabbed in (a.k.a. 
indented). The <TT>!=</TT> means does not equal so <TT>while a != 0 :</TT> means 
until a is zero run the tabbed in statements that are afterwards. 
<P>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: <PRE>while 1 == 1:
     print "Help, I'm stuck in a loop."
</PRE>
<P>This program will output <TT>Help, I'm stuck in a loop.</TT> until the heat 
death of the universe or you stop it. 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.) 
<P>
<H1><A name=SECTION00620000000000000000>Examples</A> </H1>
<P>Fibonnacci.py <PRE>#This program calulates the fibonnacci sequence
a = 0
b = 1
count = 0
max_count = 20
while count &lt; max_count:
    count = count + 1
    #we need to keep track of a since we change it
    old_a = a
    old_b = b
    a = old_b
    b = old_a + old_b
    #Notice that the , at the end of a print statement keeps it
    # from switching to a new line
    print old_a,
print
</PRE>
<P>Output: <PRE>0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181
</PRE>
<P>Password.py <PRE># Waits until a password has been entered.  Use control-C to break out with out
# the password

#Note that this must not be the password so that the 
# while loop runs at least once.
password = "foobar"

#note that != means not equal
while password != "unicorn":
    password = raw_input("Password:")
print "Welcome in"
</PRE>
<P>Sample run: <PRE>Password:auo
Password:y22
Password:password
Password:open sesame
Password:unicorn
Welcome in
</PRE>
<P>
<HR>
<!--Navigation Panel--><A 
href="http://www.honors.montana.edu/~jjc/easytut/easytut/node7.html" 
name=tex2html214><IMG align=bottom alt=next border=0 height=24 
src="node6_files/next.png" width=37></A> <A 
href="http://www.honors.montana.edu/~jjc/easytut/easytut/easytut.html" 
name=tex2html210><IMG align=bottom alt=up border=0 height=24 
src="node6_files/up.png" width=26></A> <A 
href="http://www.honors.montana.edu/~jjc/easytut/easytut/node5.html" 
name=tex2html204><IMG align=bottom alt=previous border=0 height=24 
src="node6_files/prev.png" width=63></A> <A 
href="http://www.honors.montana.edu/~jjc/easytut/easytut/node2.html" 
name=tex2html212><IMG align=bottom alt=contents border=0 height=24 
src="node6_files/contents.png" width=65></A> <BR><B>Next:</B> <A 
href="http://www.honors.montana.edu/~jjc/easytut/easytut/node7.html" 
name=tex2html215>Decisions</A> <B>Up:</B> <A 
href="http://www.honors.montana.edu/~jjc/easytut/easytut/easytut.html" 
name=tex2html211>Non-Programmers Tutorial For Python</A> <B>Previous:</B> <A 
href="http://www.honors.montana.edu/~jjc/easytut/easytut/node5.html" 
name=tex2html205>Who Goes There?</A> &nbsp; <B><A 
href="http://www.honors.montana.edu/~jjc/easytut/easytut/node2.html" 
name=tex2html213>Contents</A></B> <!--End of Navigation Panel-->
<ADDRESS>Josh Cogliati <A 
href="mailto:jjc@honors.montana.edu">jjc@honors.montana.edu</A> 
</ADDRESS></BODY></HTML>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -