Python编程:从入门到实践——练习题答案(第七章)

本文提供了一系列Python代码示例,展示了如何使用while循环获取用户输入并进行处理。例如,检查数字是否为10的整数倍,实现简单的对话系统,管理披萨配料,计算电影票价,以及处理字典数据进行用户调查。这些例子涵盖了基本的条件判断、列表操作和字典管理,是Python编程基础的重要组成部分。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

While循环
7-3 10的整数倍

print("please input a number.")
num=int(input())
if num%10==0:
    print ("是10 的整数倍")
else:
print("不是10 的整数倍")

7.2.3例子

promt="\nTell me something ,and i will repeat it back to you:"
promt+="\nEnter 'quit'to end the program"
messege=""
while messege!='quit':
    messege=input(promt)
    if messege!='quit':
        print(messege)

7-4披萨配料加入

promt="\n请输入披萨配料:"
promt+="\nEnter 'quit'to end the program"
messege=""
while messege!='quit':
    messege=input(promt)
    if messege!='quit':
        print("we will add "+messege)

7-5告诉电影票价

promt="\nplease give your old:"
promt+="\nEnter 'quit'to end the program"
messege=""
while messege!='quit':
    messege=input(promt)
    if int(messege)<3:
        print("free")
    elif int(messege)<12:
        print("10美元")
    else:
        print("15美元")

7-6

promt="\nplease give your old:"
promt+="\nEnter 'quit'to end the program"
messege=""
active=True#设置标志
while active:
    messege = input(promt)
    if messege=='quit':
        active=False
    else:

        if int(messege)<3:
            print("free")
        elif int(messege)<12:
            print("10美元")
        else:
            print("15美元")

7.3.1例子

unconfirmed_users=['a','b','c']
confirmed_users=[]
while unconfirmed_users:
    current_user=unconfirmed_users.pop()
    confirmed_users.append(current_user)
print("\nThe following users have been confirmed:")
for confirmed_user in confirmed_users:
print(confirmed_user.title())
7.3.2例子
pets=['cat','dog','dog','cat','fish','rabbit','cat']
print(pets)
while 'cat' in pets:
    pets.remove('cat')
print(pets)

7.3.3例子

responses={}
#设置标志代表调查是否继续
polling_active=True
while polling_active:
    name=input("\nwhats your name ?")
    response=input("Which one would you like to climb someday?")
    responses[name]=response
    repeat=input("Would you like to let another people respond?(yes or no)")
    if repeat =='no':
        polling_active=False
print("\n----Result----\n")
for name,response in responses.items():
print(name+" would like to climb "+response+". ")

7-9五香牛肉卖完了

print("pastrami has sold out.")
sandwich_orders=['c','pastrami','a','b','pastrami','pastrami']
finished_sandwich=[]
while 'pastrami' in sandwich_orders:
    sandwich_orders.remove('pastrami')
while sandwich_orders:
    current=sandwich_orders.pop()
    finished_sandwich.append(current)
    print("\nI made you "+current+". ")
for sand in finished_sandwich:
    print(sand,end=" ")

7-10调查用户梦想的度假胜地

#建立字典储存
responses={}
#设置结束控制量
active=True
#输入名字:地点
while active:
    name=input("\nWhat's your name ?")
    place=input("Where would you want to go?")
    responses[name]=place
    i=input("Do you want to continue?(yes/no)")
    if i=='no':
        active=False
for name,place in responses.items():
print(name+" want to go to "+place+". ")
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值