1. 程式人生 > >python day09作業答案

python day09作業答案

2.
def lst(input):
    lst2=[]
    count=0
    for i in range(0,len(input)):
        if i %2!=0:
            lst2.append(input[i])
    return lst2
a=lst((2,9,29,36,9))
print(a)


3.
def pd(input):
    if len(input)>5:
        return True
    else:
        return False
a=pd((5,5,6,9,7))
print(a)

4.
def a(input): if type(input)==list: if len(input)>2: return input[0],input[1] else: return ('請輸入列表') d=a(['小明','大明']) print(d) 5.寫函式,計算傳入函式的字串中, 數字、字母、空格 以及 其他內容的個數,並返回結果。 def func(s=""): # function shuzi = 0 zimu = 0 kongge = 0 qita = 0 for c in
s: # 迴圈字串.拿到每個字元 if c.isdigit(): # 數字 shuzi += 1 elif c.isalpha(): zimu+=1 elif c == ' ': kongge += 1 else: qita += 1 return shuzi, zimu, kongge, qita 6. def num(n1,n2): if n1>n2: return n1 elif n1<n2:
return n2 a=num(26,98) print(a) 7. def dic(dic): for k,v in dic.items(): if len(v)>2: dic[k]=v[0:2] return dic a=dic({1:'dsfgasdf',2:'sdfsdsd',3:'卡卡卡的雙豐收'}) print(a) 8. def list(input): dic={} if type(input)==type([]): for i in range(0,len(input)): dic[i]=input[i] return dic elif type(input)!=type([]): return '請輸入列表' a=list([2,3,6,5,9,8]) print(a) 9. def xx(姓名=input('姓名:'),性別=input('性別:'),年齡=input('年齡:'),學歷=input('學歷:')): student_msg={} student_msg['姓名']=姓名 student_msg['性別'] = 性別 student_msg['年齡'] = 年齡 student_msg['學歷'] = 學歷 return student_msg a=xx() print(a) 9. def func(name, age,edu,sex =""): f = open("student.msg", mode="a", encoding="utf-8") f.write(name+"_"+str(age)+"_"+sex+"_"+edu+"\n") f.flush() f.close() 10. def xx(): while True: a=input('姓名:') if a.lower() == 'q': break else: student_msg={} student_msg['姓名']=a student_msg['性別'] = input('性別:') student_msg['年齡'] = input('年齡:') student_msg['學歷'] = input('學歷:') return student_msg aa=xx() print(aa) 11. def ch(wjm,ynr,xnr): import os with open(wjm,'r',encoding='utf-8') as f1, open('a.txt','w',encoding='utf-8') as f2: for i in f1.readlines(): a=i.replace(ynr,xnr) f2.write(a) os.remove(wjm) os.rename('a.txt',wjm) ch('4.txt','alex','sb') 12. def dl(username,password): count=0 while count<=3: if username=='18329042599' and password=='199348': return ('登入成功') count=count+1 else : return ('請重新輸入:') count+=1 a=dl(12563,'4646') print(a)