1. 程式人生 > >day 10 函式 作業

day 10 函式 作業

#1.定義一個說話的函式,
# 如果別人呼叫這個函式的時候傳遞的是“你好”,就列印“你好中國人”,
# 如果傳遞的是"hello"就列印"你好外國人"。
# 方法(1)
human = input("你說話吖")
def some_people(human):
    for i in human: if 0x4E00 <= ord(i) <= 0x9FA5: print("你好,中國人") elif 0x0041 <= ord(i) <= 0x005A or 0x0061 <= ord(i) <= 0x007A: print("外國人,你好") break some_people(human) print(
"====================") # 方法(2) human = input("你說話吖") def some_people(human): if b'\xe4\xb8\x80' <= human.encode("UTF-8") <= b'\xe9\xbe\xa5': print("你好,中國人") elif b'A' <= human.encode("UTF-8") <= b'Z' or b'a' <= human.encode("UTF-8") <= b'z': print("外國人,你好") some_people(human) print(
"====================") # #2.寫一個求任意多個數之差的函式 def sub1(*q): i = q[0] - q[1] for z in range(2, len(q)): i = i - q[z] print(i) sub1(1000,5,546) # 3. 案例效果(備註:我知道德瑪西亞不耗藍) # 思路: # 1. 定義3個變數,2個是釋放技能所說的話,分別是:1:移動速度增加!攻擊造成沉默!德瑪西亞!2:我是小陀螺,刷刷刷轉起來 3:法力值,法力的初始值為100。 # 2. 寫一個釋放技能的函式,函式有2個引數1個為釋放技能所說的話1個為釋放這個技能所消耗的法力值。德瑪西亞10傷害 小陀螺20傷害 # 3. 變數函式定好之後加迴圈判做出案例效果。 skill = {
1: "移動速度增加!攻擊造成沉默!德瑪西亞", 2: "我是小陀螺,刷刷刷轉起來 !"} magic = 100 print("當前法力值", magic) def xuanze(user ): global magic while magic > 0: if user is not 1 and user is not 2: print("輸入錯誤") user = int(input("請選擇你要釋放的技能," "1:德瑪西亞 " "2:小陀螺")) if user == 1: if magic - 10 >= 0: print(skill[1]) magic -= 10 print("當前法力值", magic) if magic <= 0: print("法力值消耗完畢,程式結束") break else: user = int(input("請選擇你要釋放的技能," "1:德瑪西亞 " "2:小陀螺")) else: print("當前法力不夠:10不能釋放技能") user = int(input("請選擇你要釋放的技能," "1:德瑪西亞 " "2:小陀螺")) if magic <= 0: print("法力值消耗完畢,程式結束") break continue if user == 2: if magic - 20 >= 0: print(skill[2]) magic -= 20 print("當前法力值", magic) if magic <= 0: print("法力值消耗完畢,程式結束") break else: user = int(input("請選擇你要釋放的技能," "1:德瑪西亞 " "2:小陀螺")) else: print("當前法力不夠:20不能釋放技能") user = int(input("請選擇你要釋放的技能," "1:德瑪西亞 " "2:小陀螺")) continue user = int(input("請選擇你要釋放的技能," "1:德瑪西亞 " "2:小陀螺")) xuanze(user)