今天學習了Python函數的用法,瞭解了使用Python如何定義一個函數。
而且代碼編寫過程中也遇到了一些小小的錯誤,特此記錄一下,以方便以後在遇到同樣錯誤時能夠快速找到問題的點。
# --coding: utf-8 --
# 定義4個簡單的函數,分別是加、減、乘、除,定義函數要使用def這個關鍵字
def add(a,b): # 使用def關鍵字定義了add這個函數,給add函數指定兩個參數a和b
print "ADDing %d + %d" %(a,b) # 打印出函數中的兩個變量
return a + b #利用return語句來返回函數的結果
# 以下3個函數的說明同add函數,就不贅述了
def subtract(a, b):
print "SUBTRACTING %d - %d" % (a, b)
return a - b
def multiply(a, b):
print "MULTIPLYING %d * %d" % (a, b)
return a * b
def divide(a, b):
print "DIVIDING %d / %d" % (a, b)
return a / b
print "Let's do some math with just functions!"
age = add(300, 5) #使用add函數給age變量賦值,所得的值就是add函數中兩個變量通過函數return後的結果
height = subtract(78, 4)
weight = multiply(90, 2)
iq = divide(4, 2)
print "Age: %d, Height: %d, Weight: %d, IQ: %d" % (age, height, weight, iq)
# A puzzle for the extra credit, type it in anyway.
print "Here is a puzzle."
'''
下面這個公式稍稍有點複雜,應該從內往外開始讀
解釋一下這個公式:
1. 上面分別通過函數計算得到了age、height、weight、iq的值
2. 然後把這些值套用到函數中得到下面這樣一個數學表達式
3. what = 35 + 74 - 180 * 50 / 2
4. 得到的結果就是-4391
'''
what = add(age, subtract(height, multiply(weight, divide(iq, 2))))
print "That becomes: ", what, "Can you do it by hand?"
運行結果如下:
PS C:\Users\stephen\Desktop\python> python no21return.py
Let's do some math with just functions!
ADDing 300 + 5
SUBTRACTING 78 - 4
MULTIPLYING 90 * 2
DIVIDING 4 / 2
Age: 305, Height: 74, Weight: 180, IQ: 2
Here is a puzzle.
DIVIDING 2 / 2
MULTIPLYING 180 * 1
SUBTRACTING 74 - 180
ADDing 305 + -106
That becomes: 199 Can you do it by hand?
課程中有練習要求使用正常的方法來實現和what表達式一樣的功能,不知道我的理解是否正確,以下是個人的理解:就是使用簡單的數學表達式來完成what的賦值。
age = 30+5
height = 78-4
weight = 90*2
iq = 100/2
print "age is: %d" % age
print "height is: %d" % height
print "weight is: %d" % weight
print "iq is: %d" % iq
what = age + height - weight * iq / 2
print "what = age+height-weight*iq/2: %d = %d + %d - %d * %d /2 " % (what, age, height, weight, iq )
在代碼測試中遇到的問題如下:
-
語法錯誤。我在定義add函數的時候,在函數尾部掉了冒號
PS C:\Users\stephen\Desktop\python> python .\No21return.py File ".\No21return.py", line 1 def add(a,b) ^ SyntaxError: invalid syntax
- “return”的時候參數寫錯了。因爲使用的是Notepad++,這個軟件針對各種語言可以自動彈出語言所自定義的函數以及你在當前腳本中定義過的變量,優點是可以加快編寫代碼的速度,缺點是如果不注意會自動添加不必要的內容,比如python,你輸入"d",關於d的內建函數就會自動給你顯示一個列表。請注意我代碼中的第7行,我在"return a - b"的時候無意中把b輸入成了basestring,因此python提示整型不能和一個函數類型進行數學運算,不支持。
PS C:\Users\stephen\Desktop\python> python .\No21return.py
Let’s do some math with just functions!
ADDing 30 + 5
SUBTRACTING 78 - 4
Traceback (most recent call last):
File ".\No21return.py", line 20, in <module>
height = subtract(78, 4)
File ".\No21return.py", line 7, in subtract
return a - basestring
TypeError: unsupported operand type(s) for -: 'int' and 'type'
- print的時候漏掉了"%d"(格式化字符串)。打印的時候給定了變量的值,但是print沒有輸出。
PS C:\Users\stephen\Desktop\python> python .\No21return-1.py age is: 35 height is: 74 weight is: 180 iq is: 50 Traceback (most recent call last): File “.\No21return-1.py”, line 13, in <module> print "what = age+height-weight*iq/2: " % what TypeError: not all arguments converted during string formatting
同樣的錯誤,在代碼中少加了一個格式化字符串,因爲格式化輸出太多了,粗心漏掉了。
第13行代碼中,應有5個輸出,但在print內容裏只寫了4個。PS C:\Users\stephen\Desktop\python> python .\No21return-1.py age is: 35 height is: 74 weight is: 180 iq is: 50 Traceback (most recent call last): File ".\No21return-1.py", line 13, in <module> print "what = age+height-weight*iq/2: %d + %d - %d * %d /2 " % (age, height, weight, iq, what) TypeError: not all arguments converted during string formatting
小tips:在Linux中可以使用cat命令來查看文檔的內容,在Windows的Powershell中也有同樣的命令:" Get-Content " 。
Get-Content,獲取指定位置的項的內容。
語法:Get-Content [-Path] <文件路徑>
[-Path]由方括號引起,表示可以寫,也可以不寫;不寫則默認後面是文件路徑,寫了就指名道姓的說後面是文件路徑。