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

📄 node9.html

📁 Pythone Library reference. it is ok and simple.
💻 HTML
📖 第 1 页 / 共 2 页
字号:
<P><B>Question: </B>And what does <CODE>mult(3,2)</CODE> return? 
<P><B>Answer: </B>We need to do a walkthrough of the <TT>mult</TT> function to 
find out. 
<P><B>Question: </B>What happens next? 
<P><B>Answer: </B>The variable <TT>a</TT> gets the value 3 assigned to it and 
the variable <TT>b</TT> gets the value 2 assigned to it. 
<P><B>Question: </B>And then? 
<P><B>Answer: </B>The line <CODE>if b == 0:</CODE> is run. Since <TT>b</TT> has 
the value 2 this is false so the line <CODE>return 0</CODE> is skipped. 
<P><B>Question: </B>And what then? 
<P><B>Answer: </B>The line <CODE>rest = mult(a,b - 1)</CODE> is run. This line 
sets the local variable <TT>rest</TT> to the value of <CODE>mult(a,b - 
1)</CODE>. The value of <TT>a</TT> is 3 and the value of <TT>b</TT> is 2 so the 
function call is <CODE>mult(3,1)</CODE> 
<P><B>Question: </B>So what is the value of <CODE>mult(3,1)</CODE> ? 
<P><B>Answer: </B>We will need to run the function <TT>mult</TT> with the 
parameters 3 and 1. 
<P><B>Question: </B>So what happens next? 
<P><B>Answer: </B>The local variables in the <EM>new</EM> run of the function 
are set so that <TT>a</TT> has the value 3 and <TT>b</TT> has the value 1. Since 
these are local values these do not affect the previous values of <TT>a</TT> and 
<TT>b</TT>. 
<P><B>Question: </B>And then? 
<P><B>Answer: </B>Since <TT>b</TT> has the value 1 the if statement is false, so 
the next line becomes <CODE>rest = mult(a,b - 1)</CODE>. 
<P><B>Question: </B>What does this line do? 
<P><B>Answer: </B>This line will assign the value of <TT>mult(3,0)</TT> to rest. 

<P><B>Question: </B>So what is that value? 
<P><B>Answer: </B>We will have to run the function one more time to find that 
out. This time <TT>a</TT> has the value 3 and <TT>b</TT> has the value 0. 
<P><B>Question: </B>So what happens next? 
<P><B>Answer: </B>The first line in the function to run is <CODE>if b == 
0:</CODE> . <TT>b</TT> has the value 0 so the next line to run is <CODE>return 
0</CODE> 
<P><B>Question: </B>And what does the line <CODE>return 0</CODE> do? 
<P><B>Answer: </B>This line returns the value 0 out of the function. 
<P><B>Question: </B>So? 
<P><B>Answer: </B>So now we know that <TT>mult(3,0)</TT> has the value 0. Now we 
know what the line <CODE>rest = mult(a,b - 1)</CODE> did since we have run the 
function <TT>mult</TT> with the parameters 3 and 0. We have finished running 
<TT>mult(3,0)</TT> and are now back to running <TT>mult(3,1)</TT>. The variable 
<TT>rest</TT> gets assigned the value 0. 
<P><B>Question: </B>What line is run next? 
<P><B>Answer: </B>The line <CODE>value = a + rest</CODE> is run next. In this 
run of the function, <CODE>a=3</CODE> and <CODE>rest=0</CODE> so now 
<CODE>value=3</CODE>. 
<P><B>Question: </B>What happens next? 
<P><B>Answer: </B>The line <CODE>return value</CODE> is run. This returns 3 from 
the function. This also exits from the run of the function <TT>mult(3,1)</TT>. 
After <TT>return</TT> is called, we go back to running <TT>mult(3,2)</TT>. 
<P><B>Question: </B>Where were we in <TT>mult(3,2)</TT>? 
<P><B>Answer: </B>We had the variables <CODE>a=3</CODE> and <CODE>b=2</CODE> and 
were examining the line <CODE>rest = mult(a,b - 1)</CODE> . 
<P><B>Question: </B>So what happens now? 
<P><B>Answer: </B>The variable <TT>rest</TT> get 3 assigned to it. The next line 
<CODE>value = a + rest</CODE> sets <TT>value</TT> to <TT>3+3</TT> or 6. 
<P><B>Question: </B>So now what happens? 
<P><B>Answer: </B>The next line runs, this returns 6 from the function. We are 
now back to running the line <CODE>print "3*2 = ",mult(3,2)</CODE> which can now 
print out the 6. 
<P><B>Question: </B>What is happened overall? 
<P><B>Answer: </B>Basically we used two facts to calulate the multipule of the 
two numbers. The first is that any number times 0 is 0 (<TT>x * 0 = 0</TT>). The 
second is that a number times another number is equal to the first number plus 
the first number times one less than the second number (<TT>x * y = x + x * (y - 
1)</TT>). So what happens is <TT>3*2</TT> is first converted into <TT>3 + 
3*1</TT>. Then <TT>3*1</TT> is converted into <TT>3 + 3*0</TT>. Then we know 
that any number times 0 is 0 so <TT>3*0</TT> is 0. Then we can calculate that 
<TT>3 + 3*0</TT> is <TT>3 + 0</TT> which is <TT>3</TT>. Now we know what 
<TT>3*1</TT> is so we can calculate that <TT>3 + 3*1</TT> is <TT>3 + 3</TT> 
which is <TT>6</TT>. 
<P>This is how the whole thing works: 
<P><PRE>3*2
3 + 3*1
3 + 3 + 3*0
3 + 3 + 0
3 + 3
6
</PRE>
<P>These last two sections were recently written. If you have any comments, 
found any errors or think I need more/clearer explanations please email. I have 
been known in the past to make simple things incomprehensible. If the rest of 
the tutorial has made sense, but this section didn't, it is probably my fault 
and I would like to know. Thanks. 
<P>
<H1><A name=SECTION00940000000000000000>Examples</A> </H1>
<P>factorial.py <PRE>#defines a function that calculates the factorial

def factorial(n):
    if n &lt;= 1:
        return 1
    return n*factorial(n-1)

print "2! = ",factorial(2)
print "3! = ",factorial(3)
print "4! = ",factorial(4)
print "5! = ",factorial(5)
</PRE>
<P>Output: <PRE>2! =  2
3! =  6
4! =  24
5! =  120
</PRE>
<P>temperature2.py <PRE>#converts temperature to fahrenheit or celsius

def print_options():
    print "Options:"
    print " 'p' print options"
    print " 'c' convert from celsius"
    print " 'f' convert from fahrenheit"
    print " 'q' quit the program"

def celsius_to_fahrenheit(c_temp):
    return 9.0/5.0*c_temp+32

def fahrenheit_to_celsius(f_temp):
    return (f_temp - 32.0)*5.0/9.0

choice = "p"
while choice != "q":
    if choice == "c":
        temp = input("Celsius temperature:")
        print "Fahrenheit:",celsius_to_fahrenheit(temp)
    elif choice == "f":
        temp = input("Fahrenheit temperature:")
        print "Celsius:",fahrenheit_to_celsius(temp)
    elif choice != "q":
        print_options()
    choice = raw_input("option:")
</PRE>
<P>Sample Run: <PRE>&gt; python temperature2.py
Options:
 'p' print options
 'c' convert from celsius
 'f' convert from fahrenheit
 'q' quit the program
option:c
Celsius temperature:30 
Fahrenheit: 86.0
option:f
Fahrenheit temperature:60
Celsius: 15.5555555556
option:q
</PRE>
<P>area2.py <PRE>#By Amos Satterlee
print
def hello():
    print 'Hello!'

def area(width,height):
    return width*height

def print_welcome(name):
    print 'Welcome,',name

name = raw_input('Your Name: ')
hello(),
print_welcome(name)
print
print 'To find the area of a rectangle,'
print 'Enter the width and height below.'
print
w = input('Width:  ')
while w &lt;= 0:
    print 'Must be a positive number'
    w = input('Width:  ')
h = input('Height: ')
while h &lt;= 0:
    print 'Must be a positive number'
    h = input('Height: ')
print 'Width =',w,' Height =',h,' so Area =',area(w,h)
</PRE>
<P>Sample Run: <PRE>Your Name: Josh
Hello!
Welcome, Josh

To find the area of a rectangle,
Enter the width and height below.

Width:  -4
Must be a positive number
Width:  4
Height: 3
Width = 4  Height = 3  so Area = 12
</PRE>
<P>
<H1><A name=SECTION00950000000000000000>Exercises</A> </H1>
<P>Rewrite the area.py program done in <A 
href="http://www.honors.montana.edu/~jjc/easytut/easytut/node5.html#firstarea">3.2</A> 
to have a separate function for the area of a square, the area of a rectangle, 
and the area of a circle. (3.14 * radius**2). This program should include a menu 
interface. 
<P>
<HR>
<!--Navigation Panel--><A 
href="http://www.honors.montana.edu/~jjc/easytut/easytut/node10.html" 
name=tex2html259><IMG align=bottom alt=next border=0 height=24 
src="node9_files/next.png" width=37></A> <A 
href="http://www.honors.montana.edu/~jjc/easytut/easytut/easytut.html" 
name=tex2html255><IMG align=bottom alt=up border=0 height=24 
src="node9_files/up.png" width=26></A> <A 
href="http://www.honors.montana.edu/~jjc/easytut/easytut/node8.html" 
name=tex2html249><IMG align=bottom alt=previous border=0 height=24 
src="node9_files/prev.png" width=63></A> <A 
href="http://www.honors.montana.edu/~jjc/easytut/easytut/node2.html" 
name=tex2html257><IMG align=bottom alt=contents border=0 height=24 
src="node9_files/contents.png" width=65></A> <BR><B>Next:</B> <A 
href="http://www.honors.montana.edu/~jjc/easytut/easytut/node10.html" 
name=tex2html260>Lists</A> <B>Up:</B> <A 
href="http://www.honors.montana.edu/~jjc/easytut/easytut/easytut.html" 
name=tex2html256>Non-Programmers Tutorial For Python</A> <B>Previous:</B> <A 
href="http://www.honors.montana.edu/~jjc/easytut/easytut/node8.html" 
name=tex2html250>Debugging</A> &nbsp; <B><A 
href="http://www.honors.montana.edu/~jjc/easytut/easytut/node2.html" 
name=tex2html258>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 + -