python - 流程控制基础习题
程序员文章站
2024-01-26 22:10:52
#打印1 到100 的和** i = 0 s = 0 while i 54321 ret = 0 a = 12345 while a: last = a % 10 ret = ret * 10 + last a = a // 10 print(ret) ``` - ... ......
#打印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])) ```