#13.1 函数-积木 创建和使用函数
def printMyAddress():
print("Warren Sande")
print("123 Main Street")
print("Ottawa,Ontario,Canada")
print("K2m 2e9")
print()
printMyAddress()
#13.2调用函数
printMyAddress()
printMyAddress()
printMyAddress()
#13-2 向函数传递参数
def printMyAdress(myName):
print (myName) #形参 人名
print ("123 Main Street")
print ("Ottawa,Ontario,Cannada")
print ("K2M 2E9")
print()
printMyAdress("Carter Sande") #将Carter Sande传递给变量myName
printMyAdress("Warren Sande")
printMyAdress("Kyra Sande")
printMyAdress("Patricia Sande")
#13-3 带两个参数的函数
def printMyAddress (someName,houseNum):
print(someName)
print(houseNum,end="")#把门牌号和街道显示同一行
print("Main Street")
print("Ottawa,Ontario,Canada")
print("K2M 2E9")
print()
printMyAddress("Carter Sande","45")
printMyAddress("Jack Black","64")
printMyAddress("Tom Green","22")
printMyAddress("Todd White","36")
#13.4 返回值的函数 创建和使用返回值的函数
def calculateTax(price,tax_rate):
total = price +(price * tax_rate)
return total
my_price = float(input("Enter a price:"))
totalPrice = calculateTax(my_price,0.06)
print("price = ",my_price,"Total price = ",totalPrice)
#13-5 变量作用域 尝试打印一个局部变量
def calculateTax(prince,tax_rate):
total = price + (price * tax_rate)
return total
my_price = float (input("Enter a price:"))
totalPrice = calculateTax(my_price,0.06)
print("price=",my_price,"Tatal price = ",totalPrice)
print (price) #尝试打印局部变量,会出错,因为没有定义
#13-6 在函数中使用全局变量
def calculateTax(price,tax_rate):
total = price +(price * tax_rate)
print(my_price) #打印函数中的全局变量
return total
my_price = float (input("Enter a price:"))
totalPrice = calculateTax(my_price,0.006)
print("price=",my_price,"Total price = ", totalPrice)
#13-7 尝试在函数内部修改一个全局变量
def calculateTax(price,tax_rate):
total = price +(price * tax_rate)
my_price = 10000
print("my_price (inside function) = ",my_price)
return total
my_price = float (input("Enter a price:"))
totalPrice = calculateTax(my_price,0.06)
print("price = ",my_price,"Total price = ", totalPrice)
print("my_price (outside function) =",my_price)
def printMyAddress():
print("Warren Sande")
print("123 Main Street")
print("Ottawa,Ontario,Canada")
print("K2m 2e9")
print()
printMyAddress()
#13.2调用函数
printMyAddress()
printMyAddress()
printMyAddress()
#13-2 向函数传递参数
def printMyAdress(myName):
print (myName) #形参 人名
print ("123 Main Street")
print ("Ottawa,Ontario,Cannada")
print ("K2M 2E9")
print()
printMyAdress("Carter Sande") #将Carter Sande传递给变量myName
printMyAdress("Warren Sande")
printMyAdress("Kyra Sande")
printMyAdress("Patricia Sande")
#13-3 带两个参数的函数
def printMyAddress (someName,houseNum):
print(someName)
print(houseNum,end="")#把门牌号和街道显示同一行
print("Main Street")
print("Ottawa,Ontario,Canada")
print("K2M 2E9")
print()
printMyAddress("Carter Sande","45")
printMyAddress("Jack Black","64")
printMyAddress("Tom Green","22")
printMyAddress("Todd White","36")
#13.4 返回值的函数 创建和使用返回值的函数
def calculateTax(price,tax_rate):
total = price +(price * tax_rate)
return total
my_price = float(input("Enter a price:"))
totalPrice = calculateTax(my_price,0.06)
print("price = ",my_price,"Total price = ",totalPrice)
#13-5 变量作用域 尝试打印一个局部变量
def calculateTax(prince,tax_rate):
total = price + (price * tax_rate)
return total
my_price = float (input("Enter a price:"))
totalPrice = calculateTax(my_price,0.06)
print("price=",my_price,"Tatal price = ",totalPrice)
print (price) #尝试打印局部变量,会出错,因为没有定义
#13-6 在函数中使用全局变量
def calculateTax(price,tax_rate):
total = price +(price * tax_rate)
print(my_price) #打印函数中的全局变量
return total
my_price = float (input("Enter a price:"))
totalPrice = calculateTax(my_price,0.006)
print("price=",my_price,"Total price = ", totalPrice)
#13-7 尝试在函数内部修改一个全局变量
def calculateTax(price,tax_rate):
total = price +(price * tax_rate)
my_price = 10000
print("my_price (inside function) = ",my_price)
return total
my_price = float (input("Enter a price:"))
totalPrice = calculateTax(my_price,0.06)
print("price = ",my_price,"Total price = ", totalPrice)
print("my_price (outside function) =",my_price)
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/220205/viewspace-2075834/,如需转载,请注明出处,否则将追究法律责任。
转载于:http://blog.itpub.net/220205/viewspace-2075834/
1162

被折叠的 条评论
为什么被折叠?



