今天分享的是第七章的要点和部分课后习题的参考代码。
要点:
1、input()的工作原理和使用.
2、用int()来获取数值输入.
3、求模运算符%.
4、while循环的使用.
5、break的使用.
6、continue的使用.
7、用while来处理列表和字典.
扫描二维码关注公众号,回复:
1123630 查看本文章
参考代码:
7-1 略
7-2 略
7-3
>>> number = input("Please input a number:") Please input a number:23 >>> number = int(number) >>> if number % 10: ... print(str(number) + "不是10的倍数.") ... else: ... print(str(number) + "是10的倍数.") ... 23不是10的倍数.
7-4
>>> ingredient = "" >>> while ingredient != "quit": ... ingredient = input("Please input an ingredient:") ... if ingredient != "quit": ... print("We will add " + ingredient + " to the pizza") ... Please input an ingredient:tomato We will add tomato to the pizza Please input an ingredient:potato We will add potato to the pizza Please input an ingredient:quit
7-5(略,与7-4类似)
7-6
>>> ingredient = "" >>> active = 1 >>> while active: ... ingredient = input("Please input an ingredient:") ... if ingredient != "quit": ... print("We will add " + ingredient + " to the pizza") ... else: ... break; ... ... Please input an ingredient:tomato We will add tomato to the pizza Please input an ingredient:potato We will add potato to the pizza Please input an ingredient:quit >>>
7-7略
7-8
sandwich_orders = ["beef", "fish", "pork"] finished_sandwiches = [] while sandwich_orders: sand_ = sandwich_orders.pop() finished_sandwiches.append(sand_) print(finished_sandwiches)
运行结果:
['pork', 'fish', 'beef'] [Finished in 0.0s]
7-9略
7-10略