#1.定义一个说话的函数,
# 如果别人调用这个函数的时候传递的是“你好”,就打印“你好中国人”,
# 如果传递的是"hello"就打印"你好外国人"。
# 方法(1)
1 human = input("你说话吖") 2 def some_people(human): 3 for i in human: 4 if 0x4E00 <= ord(i) <= 0x9FA5: 5 print("你好,中国人") 6 elif 0x0041 <= ord(i) <= 0x005A or 0x0061 <= ord(i) <= 0x007A: 7 print("外国人,你好") 8 break 9 some_people(human) 10 print("====================") 11 # 方法(2) 12 human = input("你说话吖") 13 def some_people(human): 14 if b'\xe4\xb8\x80' <= human.encode("UTF-8") <= b'\xe9\xbe\xa5': 15 print("你好,中国人") 16 elif b'A' <= human.encode("UTF-8") <= b'Z' or b'a' <= human.encode("UTF-8") <= b'z': 17 print("外国人,你好") 18 some_people(human) 19 print("====================")
# #2.写一个求任意多个数之差的函数
1 def sub1(*q): 2 i = q[0] - q[1] 3 for z in range(2, len(q)): 4 i = i - q[z] 5 print(i) 6 sub1(1000,5,546)
# 3. 案例效果(备注:我知道德玛西亚不耗蓝)
# 思路:
# 1. 定义3个变量,2个是释放技能所说的话,分别是:1:移动速度增加!攻击造成沉默!德玛西亚!2:我是小陀螺,刷刷刷转起来 3:法力值,法力的初始值为100。
# 2. 写一个释放技能的函数,函数有2个参数1个为释放技能所说的话1个为释放这个技能所消耗的法力值。德玛西亚10伤害 小陀螺20伤害
# 3. 变量函数定好之后加循环判做出案例效果。
方法(1)
1 skill = {1: "移动速度增加!攻击造成沉默!德玛西亚", 2: "我是小陀螺,刷刷刷转起来 !"} 2 magic = 100 3 print("当前法力值", magic) 4 def xuanze(user ): 5 global magic 6 7 while magic > 0: 8 if user is not 1 and user is not 2: 9 print("输入错误") 10 user = int(input("请选择你要释放的技能," 11 "1:德玛西亚 " 12 "2:小陀螺")) 13 if user == 1: 14 if magic - 10 >= 0: 15 print(skill[1]) 16 magic -= 10 17 print("当前法力值", magic) 18 if magic <= 0: 19 print("法力值消耗完毕,程序结束") 20 break 21 else: 22 user = int(input("请选择你要释放的技能," 23 "1:德玛西亚 " 24 "2:小陀螺")) 25 else: 26 print("当前法力不够:10不能释放技能") 27 user = int(input("请选择你要释放的技能," 28 "1:德玛西亚 " 29 "2:小陀螺")) 30 if magic <= 0: 31 print("法力值消耗完毕,程序结束") 32 break 33 continue 34 if user == 2: 35 if magic - 20 >= 0: 36 print(skill[2]) 37 magic -= 20 38 print("当前法力值", magic) 39 if magic <= 0: 40 print("法力值消耗完毕,程序结束") 41 break 42 else: 43 user = int(input("请选择你要释放的技能," 44 "1:德玛西亚 " 45 "2:小陀螺")) 46 47 else: 48 print("当前法力不够:20不能释放技能") 49 user = int(input("请选择你要释放的技能," 50 "1:德玛西亚 " 51 "2:小陀螺")) 52 continue 53 54 user = int(input("请选择你要释放的技能," 55 "1:德玛西亚 " 56 "2:小陀螺")) 57 xuanze(user)
老师代码
1 str1 = "移动速度增加!攻击造成沉默!德玛西亚!" 2 str2 = "我是小陀螺,刷刷刷转起来" 3 mp = 100 4 5 6 # 释放技能的函数 7 def skill(speak, xh): 8 global mp 9 ''' 10 :param speak:保存释放技能时所说的话 11 :param xh:保存的是释放技能所消耗的法力值 12 :return:None 13 ''' 14 15 if mp>=xh: 16 print(speak) 17 # 总法力值减去消耗的法力值 18 mp -= xh 19 print("当前法力值为:%d" % mp) 20 else: 21 print("法力值不够,重新选择") 22 23 24 print("人在塔在") 25 print("当前法力值为:%d" % mp) 26 while True: 27 print("请选择你要释放的技能1.德玛西亚2.小陀螺") 28 num = int(input()) 29 if num == 1: 30 # 释放德玛西亚的技能 31 skill(str1, 10) 32 elif num == 2: 33 skill(str2, 20) 34 if mp <= 0: 35 print("消耗完毕") 36 break