|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
各位大神好
看大蛇的书,自己想到的一个例子(书76页,)
点比萨, 可以提供的添加有火腿和芝士
客户要求的是 蘑菇,芝士,青椒,火腿
想要输出店铺可以提供的topping的要素
available_toppings = ['火腿','芝士']
requested_toppings = ['蘑菇','芝士','青椒','火腿']
for requested_topping in requested_toppings:
if requested_topping in available_toppings:
print(f'添加{requested_topping}')
else:
print(f'对不起, {requested_topping}现在没有')
print('\n您的比萨已经搞定!')
输出结果为
对不起, 蘑菇现在没有
添加芝士
对不起, 青椒现在没有
添加火腿
您的比萨已经搞定!
问题
1. 请问我这么做对吗?
2. 如何让下面输出的顺序为 ,添加的在一起, 没有库存的在一起
例如:
添加芝士
添加火腿
对不起, 蘑菇现在没有
对不起, 青椒现在没有
您的比萨已经搞定!
谢谢
- available_toppings = ['火腿','芝士']
- requested_toppings = ['蘑菇','芝士','青椒','火腿']
- y=[]
- n=[]
- for requested_topping in requested_toppings:
- if requested_topping in available_toppings:
- y.append(requested_topping)
- else:
- n.append(requested_topping)
- for i in y:
- print(f'添加{i}')
- for i in n:
- print(f'对不起, {i}现在没有')
- print('\n您的比萨已经搞定!')
复制代码
|
|