?? node5.html
字號(hào):
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<!-- saved from url=(0061)http://www.honors.montana.edu/~jjc/easytut/easytut/node5.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>Who Goes There?</TITLE>
<META content="Who Goes There?" 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="node5_files/easytut.css" rel=STYLESHEET><LINK href="node6.html"
rel=next><LINK href="node4.html" rel=previous><LINK href="easytut.html"
rel=up><LINK href="node6.html" rel=next></HEAD>
<BODY><!--Navigation Panel--><A
href="http://www.honors.montana.edu/~jjc/easytut/easytut/node6.html"
name=tex2html199><IMG align=bottom alt=next border=0 height=24
src="node5_files/next.png" width=37></A> <A
href="http://www.honors.montana.edu/~jjc/easytut/easytut/easytut.html"
name=tex2html195><IMG align=bottom alt=up border=0 height=24
src="node5_files/up.png" width=26></A> <A
href="http://www.honors.montana.edu/~jjc/easytut/easytut/node4.html"
name=tex2html189><IMG align=bottom alt=previous border=0 height=24
src="node5_files/prev.png" width=63></A> <A
href="http://www.honors.montana.edu/~jjc/easytut/easytut/node2.html"
name=tex2html197><IMG align=bottom alt=contents border=0 height=24
src="node5_files/contents.png" width=65></A> <BR><B>Next:</B> <A
href="http://www.honors.montana.edu/~jjc/easytut/easytut/node6.html"
name=tex2html200>Count to 10</A> <B>Up:</B> <A
href="http://www.honors.montana.edu/~jjc/easytut/easytut/easytut.html"
name=tex2html196>Non-Programmers Tutorial For Python</A> <B>Previous:</B> <A
href="http://www.honors.montana.edu/~jjc/easytut/easytut/node4.html"
name=tex2html190>Hello, World</A> <B><A
href="http://www.honors.montana.edu/~jjc/easytut/easytut/node2.html"
name=tex2html198>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/node5.html#SECTION00510000000000000000"
name=tex2html201>Input and Variables</A>
<LI><A
href="http://www.honors.montana.edu/~jjc/easytut/easytut/node5.html#SECTION00520000000000000000"
name=tex2html202>Examples</A>
<LI><A
href="http://www.honors.montana.edu/~jjc/easytut/easytut/node5.html#SECTION00530000000000000000"
name=tex2html203>Exercises</A> </LI></UL><!--End of Table of Child-Links-->
<HR>
<H1><A name=SECTION00500000000000000000>Who Goes There?</A> </H1>
<H1><A name=SECTION00510000000000000000>Input and Variables</A> </H1>Now I feel
it is time for a really complicated program. Here it is: <PRE>print "Halt!"
s = raw_input("Who Goes there? ")
print "You may pass,", s
</PRE>
<P>When <B>I</B> ran it here is what <B>my</B> screen showed: <PRE>Halt!
Who Goes there? Josh
You may pass, Josh
</PRE>
<P>Of course when you run the program your screen will look different because of
the <CODE>raw_input</CODE> statement. When you ran the program you probably
noticed (you did run the program, right?) how you had to type in your name and
then press Enter. Then the program printed out some more text and also your
name. This is an example of input. The program reaches a certain point and then
waits for the user to input some data that the program can use later.
<P>Of course, getting information from the user would be useless if we didn't
have anywhere to put that information and this is where variables come in. In
the previous program <TT>s</TT> is a variable. Variables are like a box that can
store some piece of data. Here is a program to show examples of variables: <PRE>a = 123.4
b23 = 'Spam'
first_name = "Bill"
b = 432
c = a + b
print "a + b is", c
print "first_name is", first_name
print "Sorted Parts, After Midnight or",b23
</PRE>
<P>And here is the output: <PRE>a + b is 555.4
first_name is Bill
Sorted Parts, After Midnight or Spam
</PRE>
<P>Variables store data. The variables in the above program are <TT>a</TT>,
<TT>b23</TT>, <CODE>first_name</CODE>, <TT>b</TT>, and <TT>c</TT>. The two basic
types are strings and numbers. Strings are a sequence of letters, numbers and
other characters. In this example <TT>b23</TT> and <CODE>first_name</CODE> are
variables that are storing strings. <TT>Spam</TT>, <TT>Bill</TT>, <TT>a + b
is</TT>, and <CODE>first_name is</CODE> are the strings in this program. The
characters are surrounded by <TT>"</TT> or <TT>'</TT>. The other type of
variables are numbers.
<P>Okay, so we have these boxes called variables and also data that can go into
the variable. The computer will see a line like <CODE>first_name = "Bill"</CODE>
and it reads it as Put the string <TT>Bill</TT> into the box (or variable)
<CODE>first_name</CODE>. Later on it sees the statement <TT>c = a + b</TT> and
it reads it as Put <TT>a + b</TT> or <TT>123.4 + 432</TT> or <TT>555.4</TT> into
<TT>c</TT>.
<P>Here is another example of variable usage: <PRE>a = 1
print a
a = a + 1
print a
a = a * 2
print a
</PRE>
<P>And of course here is the output: <PRE>1
2
4
</PRE>
<P>Even if it is the same variable on both sides the computer still reads it as:
First find out the data to store and than find out where the data goes.
<P>One more program before I end this chapter: <PRE>num = input("Type in a Number: ")
str = raw_input("Type in a String: ")
print "num =", num
print "num is a ",type(num)
print "num * 2 =",num*2
print "str =", str
print "str is a ",type(str)
print "str * 2 =",str*2
</PRE>
<P>The output I got was: <PRE>Type in a Number: 12.34
Type in a String: Hello
num = 12.34
num is a <type 'float'>
num * 2 = 24.68
str = Hello
str is a <type 'string'>
str * 2 = HelloHello
</PRE>
<P>Notice that <CODE>num</CODE> was gotten with <TT>input</TT> while
<CODE>str</CODE> was gotten with <CODE>raw_input</CODE>. <CODE>raw_input</CODE>
returns a string while <TT>input</TT> returns a number. When you want the user
to type in a number use <TT>input</TT> but if you want the user to type in a
string use <CODE>raw_input</CODE>.
<P>The second half of the program uses <TT>type</TT> which tells what a variable
is. Numbers are of type <TT>int</TT> or <TT>float</TT> (which are short for
'integer' and 'floating point' respectively). Strings are of type
<TT>string</TT>. Integers and floats can be worked on by mathematical functions,
strings cannot. Notice how when python multiples a number by a integer the
expected thing happens. However when a string is multiplied by a integer the
string has that many copies of it added i.e. <CODE>str * 2 = HelloHello</CODE>.
<P>
<H1><A name=SECTION00520000000000000000>Examples</A> </H1>
<P>Rate_times.py <PRE>#This programs calculates rate and distance problems
print "Input a rate and a distance"
rate = input("Rate:")
distance = input("Distance:")
print "Time:",distance/rate
</PRE>
<P>Sample runs: <PRE>> python rate_times.py
Input a rate and a distance
Rate:5
Distance:10
Time: 2
> python rate_times.py
Input a rate and a distance
Rate:3.52
Distance:45.6
Time: 12.9545454545
</PRE>
<P>Area.py <A name=firstarea></A><PRE>#This program calculates the perimeter and area of a rectangle
print "Calculate information about a rectangle"
length = input("Length:")
width = input("Width:")
print "Area",length*width
print "Perimeter",2*length+2*width
</PRE>
<P>Sample runs: <PRE>> python area.py
Calculate information about a rectangle
Length:4
Width:3
Area 12
Perimeter 14
> python area.py
Calculate information about a rectangle
Length:2.53
Width:5.2
Area 13.156
Perimeter 15.46
</PRE>
<P>temperature.py <PRE>#Converts Fahrenheit to Celsius
temp = input("Farenheit temperature:")
print (temp-32.0)*5.0/9.0
</PRE>
<P>Sample runs: <PRE>> python temperature.py
Farenheit temperature:32
0.0
> python temperature.py
Farenheit temperature:-40
-40.0
> python temperature.py
Farenheit temperature:212
100.0
> python temperature.py
Farenheit temperature:98.6
37.0
</PRE>
<P>
<H1><A name=SECTION00530000000000000000>Exercises</A> </H1>
<P>Write a program that gets 2 string variables and 2 integer variables from the
user, concatenates (joins them together with no spaces) and displays the
strings, then multiplies the two numbers on a new line.
<P>
<HR>
<!--Navigation Panel--><A
href="http://www.honors.montana.edu/~jjc/easytut/easytut/node6.html"
name=tex2html199><IMG align=bottom alt=next border=0 height=24
src="node5_files/next.png" width=37></A> <A
href="http://www.honors.montana.edu/~jjc/easytut/easytut/easytut.html"
name=tex2html195><IMG align=bottom alt=up border=0 height=24
src="node5_files/up.png" width=26></A> <A
href="http://www.honors.montana.edu/~jjc/easytut/easytut/node4.html"
name=tex2html189><IMG align=bottom alt=previous border=0 height=24
src="node5_files/prev.png" width=63></A> <A
href="http://www.honors.montana.edu/~jjc/easytut/easytut/node2.html"
name=tex2html197><IMG align=bottom alt=contents border=0 height=24
src="node5_files/contents.png" width=65></A> <BR><B>Next:</B> <A
href="http://www.honors.montana.edu/~jjc/easytut/easytut/node6.html"
name=tex2html200>Count to 10</A> <B>Up:</B> <A
href="http://www.honors.montana.edu/~jjc/easytut/easytut/easytut.html"
name=tex2html196>Non-Programmers Tutorial For Python</A> <B>Previous:</B> <A
href="http://www.honors.montana.edu/~jjc/easytut/easytut/node4.html"
name=tex2html190>Hello, World</A> <B><A
href="http://www.honors.montana.edu/~jjc/easytut/easytut/node2.html"
name=tex2html198>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>
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -