栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

python编程:从入门到实践 第五章知识汇总 + 习题7-1~7-10

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

python编程:从入门到实践 第五章知识汇总 + 习题7-1~7-10

 章节回顾:

1. 如何在程序中使用input() 来让用户提供信息;

2. 如何处理文本和数字输入;

3. 如何使用while 循环让程序按用户的要求不断地运行;

4. 多种控制while 循环流程的方式:设置活动标志、使用break 语句以及使用continue 语句;

5. 如何使用while 循环在列表之间移动元素;

6. 如何从列表中删除所有包含特定值的元素;

7.如何结合使用while 循环和字典。

7-1 汽车租赁: 编写一个程序,询问用户要租赁什么样的汽车,并打印一条消息,如 “Let me see if I can find you a Subaru” 。
car = input("what type of car would you like ?")
print("Let me see if I can find you a " + car + ".")
what type of car would you like ?ju
Let me see if I can find you a ju.
7-2 参观订位: 编写一个程序,询问用户有多少人用餐。如果超过 8 人,就打印一条消息,指出没有空桌;否则指出有空桌。
number = input("How many will join in the party?")
number = int(number)
if number > 8:
    print(" Sorry, wo don`t have enough table.")
else:
    print(" Ok, we have enough table, please come.")
7-3 10的整数倍数: 让用户输入一个数字,并指出这个数字是否是 10 的整数倍。
number = int(input("Please input your lucky number: "))
if number % 10 == 0:
    print(str(number) + " is multiple of ten.")
else:
    print(str(number) + " is not multiple of ten.")
Please input your lucky number: 100
100 is multiple of ten.

Process finished with exit code 0


Please input your lucky number: 13
13 is not multiple of ten.

Process finished with exit code 0
7-4 比萨配料: 编写一个循环,提示用户输入一系列的比萨配料,并在用户输入 'quit' 时结束循环。每当用户输入一种配料后,都打印一条消息,说我们会在比萨 中添加这种配料。
topping = "nPlease enter what type of topping would you like: "
topping += "n(Enter 'quit' when you are finished.)"

# 方法一:
message = ""
while message != 'quit':
    message = input(topping)

    if message != 'quit':
        print(message)


# 方法二:
topping = "nPlease enter what type of topping would you like: "
topping += "n(Enter 'quit' when you are finished.)"

while True:
    pizza = input(topping)
    if pizza == 'quit':
        break
    else:
        print("We will add " + pizza + " on the pizza!")


# 方法3:
topping = "nPlease enter what type of topping would you like: "
topping += "n(Enter 'quit' when you are finished.)"

active = True
while active:
    message = input(topping)
    if message == 'quit':
        active = False
    else:
        print(message)
Please enter what type of topping would you like: 
(Enter 'quit' when you are finished.)13
13

Please enter what type of topping would you like: 
(Enter 'quit' when you are finished.)quit

Process finished with exit code 0
7-5 电影票: 有家电影院根据观众的年龄收取不同的票价:不到 3 岁的观众免费; 3~12 岁的观众为 10 美元;超过 12 岁的观众为 15 美元。请编写一个循环,在其中询问用 户的年龄,并指出其票价。
prompt = "n please enter your age: "
prompt += "n(Enter 'quit' when you are finished.)"

# 方法一:break的退出方式:
while True:
    age = input(prompt)
    if age == 'quit':
        break
    else:
        age = int(age)
        if age < 3:
            print("Free")
        elif 3 <= age <= 12:
            print("The ticket prince is $10 per person.")
        else:
            print("The ticket prince is $15 per person.")

prompt = "n please enter your age: "
prompt += "n(Enter 'quit' when you are finished.)"
# 方法二 标志的退出方式
active = True
while active:
    age = input(prompt)
    if age == 'quit':
        active = False
    else:
        age = int(age)
        if age < 3:
            print("Free")
        elif 3 <= age <= 12:
            print("The ticket prince is $10 per person.")
        else:
            print("The ticket prince is $15 per person.")
please enter your age: 
(Enter 'quit' when you are finished.)15
The ticket prince is $15 per person.

 please enter your age: 
(Enter 'quit' when you are finished.)0
Free

 please enter your age: 
(Enter 'quit' when you are finished.)2
Free

 please enter your age: 
(Enter 'quit' when you are finished.)quit

Process finished with exit code 0
7-6 三个出口: 以另一种方式完成练习 7-4 或练习 7-5 ,在程序中采取如下所有做法。 在 while 循环中使用条件测试来结束循环。 使用变量 active 来控制循环结束的时机。 使用 break 语句在用户输入 'quit' 时退出循环。         已经在上面给出结果。 7-7 无限循环: 编写一个没完没了的循环,并运行它(要结束该循环,可按 Ctrl+C ,也可关闭显示输出的窗口)。
prompt = input("n please enter your age: ")

while True:

    print("111")
7-8 熟食店: 创建一个名为 sandwich_orders 的列表,在其中包含各种三明治的名字;再创建一个名为 finished_sandwiches 的空列表。遍历列表 sandwich_orders ,对于其中的每种三明治,都打印一条消息,如 I made your tuna sandwich ,并将其移到列表 finished_sandwiches 。所有三明 治都制作好后,打印一条消息,将这些三明治列出来。
sandwich_order = ['apple', 'orange', 'fish']
finished_sandwich = []
while sandwich_order:
    current_sandwich = sandwich_order.pop()
    print("I made your " + current_sandwich + " sandwich.")
    finished_sandwich.append(current_sandwich)

print("nFinally, we made these sandwich:")
for sandwich in finished_sandwich:
    print(sandwich.title())
I made your fish sandwich.
I made your orange sandwich.
I made your apple sandwich.

Finally, we made these sandwich:
Fish
Orange
Apple

Process finished with exit code 0
7-9 五香熏牛肉(pastrami)卖完了: 使用为完成练习 7-8 而创建的列表 sandwich_orders ,并确保 'pastrami' 在其中至少出现了三次。在程序开头附近添加 这样的代码:打印一条消息,指出熟食店的五香烟熏牛肉卖完了;再使用一个 while 循环将列表 sandwich_orders 中的 'pastrami' 都删除。确认最终的列表 finished_sandwiches 中不包含 'pastrami' 。  
# 方法一:错误不是题目描述的内容
sandwich_order = ['apple', 'pastrami', 'orange', 'pastrami', 'pastrami', 'fish', 'pastrami']
print("Pastrami was sold out.")
while 'pastrami' in sandwich_order:
    sandwich_order.remove('pastrami')
print("These are what we have now:")
for sandwich in sandwich_order:
    print(sandwich.title())

# 方法二:正确
# 创建一个空的列表finished_sandwich
finished_sandwich = []

# 遍历sandwich_order,并对每种三明治打印一条提示信息
for sandwich in sandwich_order:
    if sandwich == 'pastrami':
        print(sandwich.title() + " was sold out. Please order others~")
    else:
        print("nYou`re so lucky," + sandwich + " is what we have now.")
    #     对卖完的品种进行删除
    while 'pastrami' in sandwich_order:
        sandwich_order.remove('pastrami')

while sandwich_order:
    current_sandwich = sandwich_order.pop()
    # 打印列表,查看是否含有pastrami
    print("nYour " + current_sandwich + " is making now, please waiting.")
    finished_sandwich.append(current_sandwich)

print("nThese sandwiches are on the way: ")
for sandwich in finished_sandwich:
    print(sandwich.title())
# 方法一:输出
Pastrami was sold out.
These are what we have now:
Apple
Orange
Fish

Process finished with exit code 0


# 方法二:输出
Pastrami was sold out. Please order others~

You`re so lucky,orange is what we have now.

You`re so lucky,fish is what we have now.

Your fish is making now, please waiting.

Your orange is making now, please waiting.

Your apple is making now, please waiting.

These sandwiches are on the way: 
Fish
Orange
Apple

Process finished with exit code 0
7-10 梦想的度假胜地: 梦 编写一个程序,调查用户梦想的度假胜地。使用类似于 “If you could visit one placein the world, where would you go?” 的提示,并编写一个打印调查 结果的代码块。
dream_places = {}
# 设置一个标志,指出调查是否继续
active =True
while active:
    # 提示输入被调查者的名字和回答
    name = input("nHello, dear. What`s your name?")
    place = input("If you could visit one place in the world, where would you go?")
    # 将答案存储在字典中
    dream_places[name] = place
    print(dream_places)
# 看看是否还有人要参与调查
    repeat = input("Would you like to let another person respond? (yes/ no) ")
    if repeat == 'no':
        active = False
    else:
        print("Sorry, there is no seat for your friend~ ")
        active = False
# 调查结束,显示结果
print("n---  Results ---")
for name, place in dream_places.items():
    print(name.title() + " would like to visit " + place.title() + ".")
Hello, dear. What`s your name? HE JIN
If you could visit one place in the world, where would you go?SHANGHAI
{' HE JIN': 'SHANGHAI'}
Would you like to let another person respond? (yes/ no) yes
Sorry, there is no seat for your friend~ 

---  Results ---
 He Jin would like to visit Shanghai.

Process finished with exit code 0

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/341445.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号