因为编程的练习题是交互式的,在不断调试和不断渐进完善中,你会有一种成就感和快乐感,不断的修缮,不断的尝试.
其实,认知自己,和探索世界,也是这样的啊.
只要不放弃,要坚持.
#7 根据列表lt,实现输出: '我叫xxx,我来自xxx'
1 lt = [ 2 {'name':'王大花', 'age':18, 'info':[('phone', '123'), ('dizhi', '湖北武汉')]}, 3 {'name':'哈哈', 'age':19, 'info':[('phone', '789'), ('dizhi', '湖南长沙')]}, 4 {'name':'呵呵', 'age':32, 'info':[('phone', '567'), ('dizhi', '北京')]}, 5 {'name':'啦啦', 'age':28, 'info':[('phone', '000'), ('dizhi', '上海')]}, 6 {'name':'狗狗', 'age':56, 'info':[('phone', '111'), ('dizhi', '山西吕梁')]}, 7 ] 8 9 for i in lt: 10 # 格式化输出 嵌套容器中锁定目标值 11 print('我叫%s,我来自%s' %(i['name'], i['info'][1][1]))
#8 使用循环,实现模拟钟表
输入小时、分钟、秒,输出下一秒的时间
23: 59: 59
00: 00: 00
import time while True: # 输出型阻塞 hour = input('hour:') minute = input('minute:') second = input('second:') hour = int(hour) minute = int(minute) second = int(second) # 参数入口检测 if not (hour>=0 and hour<24) and not (minute>=0 and minute<60) and not (second>=0 and second<60): print('wrong, please input again') # 输入不符合规范,就跳出本次循环,重新输入 continue while True: # 1秒钟一次 second自加1 time.sleep(1) second += 1 # 考虑循环的极限节点 if second == 60: second = 0 minute += 1 # 考虑循环的极限节点 if minute ==60: minute = 0 hour += 1 # 考虑循环的极限节点 if hour == 24: hour = 0 # 格式化输出 print('%02d:%02d:%02d' %(hour, minute, second))
# 9 写一个函数,输入m, n,打印m行n列表格,表格里面的内容从1开始,先横再纵.
如输入2 5,结果如下:
1 2 3 4 5
6 7 8 9 10
1 m = input('m=') 2 n = input('n=') 3 4 m = int(m) 5 n = int(n) 6 7 count = 0 8 for i in range(1, m*n+1): 9 10 print(i, end=' ') 11 count += 1 12 if count%n==0: 13 print('')
#10 打印99乘法表
1 for i in range(1, 10): 2 for j in range(1, i+1): 3 print('%d*%d=%d ' %(int(i), int(j), int(i)*int(j)), end=' ') 4 print('')