Python 15

  1. 课上代码
#全局变量(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

二. 测试题

  1. 目测以下程序会打印什么内容
def fun(var):
    var = 1314
    print(var, end = '')
    
var = 520
fun(var)
print(var)
1314520
  1. 目测一下程序会打印什么内容
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

三. 动动手

  1. 编写一个函数,判断传入的字符串参数是否为回文联,也就是既可顺读,也可倒读,例如: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 -= 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)
#这一步想到了,但就是不知道怎么实现
    list2 = reversed(list1)
    if list1 == list(list2):
        return "Yes!"
    else:
        return "No!"
print(palindrome('abcba'))

二. 编写一个函数,分别统计处传入字符参数(可能不止一个参数)的英文字母,空格,数字和其他字符的个数

#个人代码
#还是没有写出函数,另外这只能测试一个参数
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个,数字%d个,空格%d个,其他字符%d个。' % (i + 1, letters, digit, space, others))

count('I love fishc.com.', 'I love you, you love me.')

你可能感兴趣的:(Python 15)