📄 node7.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<!-- saved from url=(0061)http://www.honors.montana.edu/~jjc/easytut/easytut/node7.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>Decisions</TITLE>
<META content=Decisions 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="node7_files/easytut.css" rel=STYLESHEET><LINK href="node8.html"
rel=next><LINK href="node6.html" rel=previous><LINK href="easytut.html"
rel=up><LINK href="node8.html" rel=next></HEAD>
<BODY><!--Navigation Panel--><A
href="http://www.honors.montana.edu/~jjc/easytut/easytut/node8.html"
name=tex2html228><IMG align=bottom alt=next border=0 height=24
src="node7_files/next.png" width=37></A> <A
href="http://www.honors.montana.edu/~jjc/easytut/easytut/easytut.html"
name=tex2html224><IMG align=bottom alt=up border=0 height=24
src="node7_files/up.png" width=26></A> <A
href="http://www.honors.montana.edu/~jjc/easytut/easytut/node6.html"
name=tex2html218><IMG align=bottom alt=previous border=0 height=24
src="node7_files/prev.png" width=63></A> <A
href="http://www.honors.montana.edu/~jjc/easytut/easytut/node2.html"
name=tex2html226><IMG align=bottom alt=contents border=0 height=24
src="node7_files/contents.png" width=65></A> <BR><B>Next:</B> <A
href="http://www.honors.montana.edu/~jjc/easytut/easytut/node8.html"
name=tex2html229>Debugging</A> <B>Up:</B> <A
href="http://www.honors.montana.edu/~jjc/easytut/easytut/easytut.html"
name=tex2html225>Non-Programmers Tutorial For Python</A> <B>Previous:</B> <A
href="http://www.honors.montana.edu/~jjc/easytut/easytut/node6.html"
name=tex2html219>Count to 10</A> <B><A
href="http://www.honors.montana.edu/~jjc/easytut/easytut/node2.html"
name=tex2html227>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/node7.html#SECTION00710000000000000000"
name=tex2html230>If statement</A>
<LI><A
href="http://www.honors.montana.edu/~jjc/easytut/easytut/node7.html#SECTION00720000000000000000"
name=tex2html231>Examples</A>
<LI><A
href="http://www.honors.montana.edu/~jjc/easytut/easytut/node7.html#SECTION00730000000000000000"
name=tex2html232>Exercises</A> </LI></UL><!--End of Table of Child-Links-->
<HR>
<H1><A name=SECTION00700000000000000000>Decisions</A> </H1>
<P>
<H1><A name=SECTION00710000000000000000>If statement</A> </H1>As always I
believe I should start each chapter with a warm up typing exercise so here is a
short program to compute the absolute value of a number: <PRE>n = input("Number? ")
if n < 0:
print "The absolute value of",n,"is",-n
else:
print "The absolute value of",n,"is",n
</PRE>
<P>Here is the output from the two times that I ran this program: <PRE>Number? -34
The absolute value of -34 is 34
Number? 1
The absolute value of 1 is 1
</PRE>
<P>So what does the computer do when when it sees this piece of code? First it
prompts the user for a number with the statement <TT>n = input("Number? ")</TT>.
Next it reads the line <TT>if n < 0:</TT> If <TT>n</TT> is less than zero
Python runs the line <TT>print "The absolute value of",n,"is",-n</TT>. Otherwise
python runs the line <TT>print "The absolute value of",n,"is",n</TT>.
<P>More formally Python looks at whether the <EM>expression</EM> <TT>n <
0</TT> is true or false. A <TT>if</TT> statement is followed by a <EM>block</EM>
of statements that are run when the expression is true. Optionally after the
<TT>if</TT> statement is a <TT>else</TT> statement. The <TT>else</TT> statement
is run if the expression is false.
<P>There are several different tests that a expression can have. Here is a table
of all of them:
<P>
<TABLE border=1 cellPadding=3>
<TBODY>
<TR>
<TD align=left>operator</TD>
<TD align=left>function</TD></TR>
<TR>
<TD align=left><CODE><</CODE></TD>
<TD align=left>less than</TD></TR>
<TR>
<TD align=left><CODE><=</CODE></TD>
<TD align=left>less than or equal to</TD></TR>
<TR>
<TD align=left><CODE>></CODE></TD>
<TD align=left>greater than</TD></TR>
<TR>
<TD align=left><CODE>>=</CODE></TD>
<TD align=left>greater than or equal to</TD></TR>
<TR>
<TD align=left><CODE>==</CODE></TD>
<TD align=left>equal</TD></TR>
<TR>
<TD align=left><CODE>!=</CODE></TD>
<TD align=left>not equal</TD></TR>
<TR>
<TD align=left><CODE><></CODE></TD>
<TD align=left>another way to say not equal</TD></TR></TBODY></TABLE>
<P>Another feature of the <TT>if</TT> command is the <TT>elif </TT>statement. It
stands for else if and means if the original <TT>if</TT> statement is false and
then the <TT>elif</TT> part is true do that part. Here's a example: <PRE>a = 0
while a < 10:
a = a + 1
if a > 5:
print a," > ",5
elif a <= 7:
print a," <= ",7
else:
print "Neither test was true"
</PRE>
<P>and the output:
<P><PRE>1 <= 7
2 <= 7
3 <= 7
4 <= 7
5 <= 7
6 > 5
7 > 5
8 > 5
9 > 5
10 > 5
</PRE>
<P>Notice how the <TT>elif a <= 7</TT> is only tested when the <TT>if</TT>
statement fail to be true. <TT>elif</TT> allows multiple tests to be done in a
single if statement.
<P>
<H1><A name=SECTION00720000000000000000>Examples</A> </H1>
<P>High_low.py <A name=firsthighlow></A><PRE>#Plays the guessing game higher or lower
# (originally written by Josh Cogliati, improved by Quique)
#This should actually be something that is semi random like the
# last digits of the time or something else, but that will have to
# wait till a later chapter. (Extra Credit, modify it to be random
# after the Modules chapter)
number = 78
guess = 0
while guess != number :
guess = input ("Guess a number: ")
if guess > number :
print "Too high"
elif guess < number :
print "Too low"
print "Just right"
</PRE>
<P>Sample run: <PRE>Guess a number:100
Too high
Guess a number:50
Too low
Guess a number:75
Too low
Guess a number:87
Too high
Guess a number:81
Too high
Guess a number:78
Just right
</PRE>
<P>even.py <PRE>#Asks for a number.
#Prints if it is even or odd
number = input("Tell me a number: ")
if number % 2 == 0:
print number,"is even."
elif number % 2 == 1:
print number,"is odd."
else:
print number,"is very strange."
</PRE>
<P>Sample runs. <PRE>Tell me a number: 3
3 is odd.
Tell me a number: 2
2 is even.
Tell me a number: 3.14159
3.14159 is very strange.
</PRE>
<P>
<H1><A name=SECTION00730000000000000000>Exercises</A> </H1>
<P>Modify the password guessing program to keep track of how many times the user
has entered the password wrong. If it is more than 3 times, print ``That must
have been complicated.''
<P>Write a program that asks for two numbers. If the sum of the numbers is
greater than 100, print ``That is big number''.
<P>Write a program that asks the user their name, if they enter your name say
"That is a nice name", if they enter "John Cleese" or "Michael Palin", tell them
how you feel about them ;), otherwise tell them "You have a nice name".
<P>
<HR>
<!--Navigation Panel--><A
href="http://www.honors.montana.edu/~jjc/easytut/easytut/node8.html"
name=tex2html228><IMG align=bottom alt=next border=0 height=24
src="node7_files/next.png" width=37></A> <A
href="http://www.honors.montana.edu/~jjc/easytut/easytut/easytut.html"
name=tex2html224><IMG align=bottom alt=up border=0 height=24
src="node7_files/up.png" width=26></A> <A
href="http://www.honors.montana.edu/~jjc/easytut/easytut/node6.html"
name=tex2html218><IMG align=bottom alt=previous border=0 height=24
src="node7_files/prev.png" width=63></A> <A
href="http://www.honors.montana.edu/~jjc/easytut/easytut/node2.html"
name=tex2html226><IMG align=bottom alt=contents border=0 height=24
src="node7_files/contents.png" width=65></A> <BR><B>Next:</B> <A
href="http://www.honors.montana.edu/~jjc/easytut/easytut/node8.html"
name=tex2html229>Debugging</A> <B>Up:</B> <A
href="http://www.honors.montana.edu/~jjc/easytut/easytut/easytut.html"
name=tex2html225>Non-Programmers Tutorial For Python</A> <B>Previous:</B> <A
href="http://www.honors.montana.edu/~jjc/easytut/easytut/node6.html"
name=tex2html219>Count to 10</A> <B><A
href="http://www.honors.montana.edu/~jjc/easytut/easytut/node2.html"
name=tex2html227>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 + -