- 課上代碼
#全局變量(global variable)和局部變量(local variable)和差別
def discounts(price, rate):
final_price = price * rate
print("這里試圖打印全局變量old_price的值:", old_price)
return final_price
old_price = float(input("請輸入原價:"))
rate = float(input('請輸入折扣率:'))
new_price = discounts(old_price, rate)
print('打折后價格是:', new_price)
>>>
=================== RESTART: C:/Users/xin/Desktop/test.py ===================
請輸入原價:100
請輸入折扣率:0.8
這里試圖打印全局變量old_price的值: 100.0
打折后價格是: 80.0
def discounts(price, rate):
final_price = price * rate
#print("這里試圖打印全局變量old_price的值:", old_price)
old_price = 50
print('修改后old_price的值1是:', old_price)
return final_price
old_price = float(input("請輸入原價:"))
rate = float(input('請輸入折扣率:'))
new_price = discounts(old_price, rate)
print('修改后old_price的值2是:',old_price)
print('打折后價格是:', new_price)
>>>
=================== RESTART: C:/Users/xin/Desktop/test.py ===================
請輸入原價:100
請輸入折扣率:0.8
修改后old_price的值1是: 50
修改后old_price的值2是: 100.0
打折后價格是: 80.0
二. 測試題
- 目測以下程序會打印什么內(nèi)容
def fun(var):
var = 1314
print(var, end = '')
var = 520
fun(var)
print(var)
1314520
- 目測一下程序會打印什么內(nèi)容
var = ' Hi '
def fun1():
global var
var = ' Baby '
return fun2(var)
def fun2(var):
var += 'I love you'
fun3(var)
return var
def fun3(var):
var = ' Jack '
print(fun1())
Baby I love you
三. 動動手
- 編寫一個函數(shù),判斷傳入的字符串參數(shù)是否為回文聯(lián)橄务,也就是既可順讀幔托,也可倒讀,例如:abcba
#個人代碼
#沒有編寫函數(shù)蜂挪,而且比較復(fù)雜重挑,需要把一句話拆成兩段再比較是不是相同
checking_string = input("Please input a sentence: ")
length = len(checking_string)
half_length = length // 2
L1 = []
L2 = []
checking_string1 = checking_string[ : (half_length + 1)]
checking_string2 = checking_string[half_length : ]
length1 = len(checking_string1)
length2 = len(checking_string2)
for i in range(0, length1):
L1.append(checking_string1[i])
for j in range(length2 - 1, -1, -1):
L2.append(checking_string2[j])
if L1 == L2:
print("Yes!")
else:
print("No!")
#參考代碼1
def palindrome(string):
length = len(string)
last = length - 1
length //= 2
flag = 1
for each in range(length):
if string[each] != string[last]:
flag = 0
last -= 1
if flag == 1:
return 1
else:
return 0
string = input("Please input a sentence: ")
if palindrome(string) == 1:
print("Yes!")
else:
print("No!")
#參考代碼2
def palindrome(string):
list1 = list(string)
#這一步想到了,但就是不知道怎么實現(xiàn)
list2 = reversed(list1)
if list1 == list(list2):
return "Yes!"
else:
return "No!"
print(palindrome('abcba'))
二. 編寫一個函數(shù)棠涮,分別統(tǒng)計處傳入字符參數(shù)(可能不止一個參數(shù))的英文字母攒驰,空格,數(shù)字和其他字符的個數(shù)
#個人代碼
#還是沒有寫出函數(shù)故爵,另外這只能測試一個參數(shù)
alphabets = 'QWERTYUIOPLKJHGFDSAZXCVBNMqwertyuioplkjhgfdsazxcvbnm'
number = '0123456789'
other_character = "~`!@#$%^&*()_+-={}|[]\:;'<>?,./"
num_alphabets = 0
num_number = 0
num_other_character = 0
num_space = 0
string = input("Please input a sentence: ")
for i in string:
if i in alphabets:
num_alphabets += 1
elif i == ' ':
num_space += 1
elif i in number:
num_number += 1
elif i in other_character:
num_other_character += 1
print(num_alphabets, num_number, num_other_character, num_space)
#參考代碼
def count(*param):
length = len(param)
for i in range(length):
letters = 0
space = 0
digit = 0
others = 0
for each in param[i]:
if each.isalpha():
letters += 1
elif each.isdigit():
digit += 1
elif each == ' ':
space += 1
else:
others += 1
print('第%d個字符串共有:英文字母%d個玻粪,數(shù)字%d個,空格%d個诬垂,其他字符%d個劲室。' % (i + 1, letters, digit, space, others))
count('I love fishc.com.', 'I love you, you love me.')