python – 流程控制基礎習題

程式灬波發表於2018-11-03
 #列印1 到100 的和**

     i = 0
     s = 0
     while i <100:
         i += 1
         s += i
     print(s)
     print(`1+2+,,100=`,s)

#列印字元A~Z
for i in range(ord(`A`),ord(`Z`)): print(chr(i)) #將12345轉換為54321 ``` #12345=>54321 ret = 0 a = 12345 while a: last = a % 10 ret = ret * 10 + last a = a // 10 print(ret) ``` - 將12345轉換為`12345`,不要使用str ``` a = 12345 ret = `` while a: #提取最後一個數字 n = a % 10 #轉為對應的字元 c = chr(n + ord(`0`)) #拼接到ret最前面 ret = c + ret #除10 為下次做準備 a = a//10 print(ret) ``` **遍歷列表,列印:我叫name,今年age歲,家住dizhi,電話phone** ``` lt = [ {`name`: `田馥甄`, `age`: `36`, `info`: [(`phone`, `1383838438`), (`address`, `北京`)]}, {`name`: `柳巖`, `age`: `33`, `info`: [(`phone`, `139809808`), (`address`, `河南鄭州`)]}, {`name`: `林志玲`, `age`: `42`, `info`: [(`phone`, `13767655465`), (`address`, `河北石家莊`)]}, {`name`: `柳慧芬`, `age`: `18`, `info`: [(`phone`, `13737623`), (`address`, `山東濟南`)]}, ] for i in lt: print(`我的名字是%s,我的年齡是%s,我的電話是%s,我的家住在%s` % (i[`name`], i[`age`], i[`info`][0][-1], i[`info`][-1][-1])) ```

 

相關文章