Python|Python 15
- 课上代码
#全局变量(global variable)和局部变量(local variable)和差别
def discounts(price, rate):
final_price = price * rate
print("这里试图打印全局变量old_price的值:", old_price)
return final_priceold_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_priceold_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
二. 测试题
- 目测以下程序会打印什么内容
def fun(var):
var = 1314
print(var, end = '')var = 520
fun(var)
print(var)
1314520
- 目测一下程序会打印什么内容
var = ' Hi 'def fun1():
global var
var = ' Baby '
return fun2(var)def fun2(var):
var += 'I love you'
fun3(var)
return vardef fun3(var):
var = ' Jack 'print(fun1())
Baby I love you
三. 动动手
- 编写一个函数,判断传入的字符串参数是否为回文联,也就是既可顺读,也可倒读,例如:abcba
#个人代码
#没有编写函数,而且比较复杂,需要把一句话拆成两段再比较是不是相同
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 -= 1if flag == 1:
return 1
else:
return 0string = input("Please input a sentence: ")
if palindrome(string) == 1:
print("Yes!")
else:
print("No!")
#参考代码2
def palindrome(string):
list1 = list(string)
#这一步想到了,但就是不知道怎么实现
list2 = reversed(list1)
if list1 == list(list2):
return "Yes!"
else:
return "No!"
print(palindrome('abcba'))
【Python|Python 15】二. 编写一个函数,分别统计处传入字符参数(可能不止一个参数)的英文字母,空格,数字和其他字符的个数
#个人代码
#还是没有写出函数,另外这只能测试一个参数
alphabets = 'QWERTYUIOPLKJHGFDSAZXCVBNMqwertyuioplkjhgfdsazxcvbnm'
number = '0123456789'
other_character = "~`!@#$%^&*()_+-={}|[]\:;
'<>?,./"
num_alphabets = 0
num_number = 0
num_other_character = 0
num_space = 0string = 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 += 1print(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个,数字%d个,空格%d个,其他字符%d个。' % (i + 1, letters, digit, space, others))count('I love fishc.com.', 'I love you, you love me.')
推荐阅读
- CVE-2020-16898|CVE-2020-16898 TCP/IP远程代码执行漏洞
- python学习之|python学习之 实现QQ自动发送消息
- 逻辑回归的理解与python示例
- python自定义封装带颜色的logging模块
- 【Leetcode/Python】001-Two|【Leetcode/Python】001-Two Sum
- Python基础|Python基础 - 练习1
- 不废话,代码实践带你掌握|不废话,代码实践带你掌握 强缓存、协商缓存!
- Python爬虫|Python爬虫 --- 1.4 正则表达式(re库)
- Python(pathlib模块)
- 工具|后天就是七夕节,你准备好了吗(送上几个七夕代码,展示你技能的时候到了!)