【Python 3.7】熟食店:创建一个名为 sandwich_orders 的列表,在其中包含各种三明治的名 字;再创建一个名为 finished_sandwiches 的空列表……

【Python 3.7】 熟食店:创建一个名为 sandwich_orders 的列表,在其中包含各种三明治的名字;再创建一个名为 finished_sandwiches 的空列表。遍历列表 sandwich_orders ,对于其中的每种三明治,都打印一条消息,如 I made your tuna sandwich ,并将其移到列表finished_sandwiches 。所有三明治都制作好后,打印一条消息,将这些三明治列出来。

题目: 熟食店:创建一个名为 sandwich_orders 的列表,在其中包含各种三明治的名字;再创建一个名为 finished_sandwiches 的空列表。遍历列表 sandwich_orders ,对于其中的每种三明治,都打印一条消息,如 I made your tuna sandwich ,并将其移到列表finished_sandwiches 。所有三明治都制作好后,打印一条消息,将这些三明治列出来。

我选择的三明治为:Tuna sandwich(金枪鱼三明治),'Ham cheese sandwich(火腿奶酪三明治),Chicken sandwich(鸡肉三明治)。
程序为:

sandwich_orders=['Tuna sandwich','Ham cheese sandwich','Chicken sandwich']
finished_sandwiches=[]
# 开始做每个三明治,直到没有三明治为止
# 将每个做完的三明治都移到已已做完三明治列表中
while sandwich_orders:
    current_orders=sandwich_orders.pop()
    print("I made your "+current_orders.title()+".")
    finished_sandwiches.append(current_orders)
#显示做完的三明治
print("\nAll the sandwiches have been finished.")
for finished_sandwiche in finished_sandwiches:
    print(finished_sandwiche.title())

结果为:

I made your Chicken Sandwich.
I made your Ham Cheese Sandwich.
I made your Tuna Sandwich.

All the sandwiches have been finished.
Chicken Sandwich
Ham Cheese Sandwich
Tuna Sandwich
  • 5
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 14
    评论
import random # sandwich ingredients and their associated calories ingredients = { "Bread": 100, "Ham": 150, "Lettuce": 10, "Tomato": 20, "Mayonnaise": 50, "Cheese": 120 } # prompt user for calorie range min_calories = int(input("Enter minimum calories: ")) max_calories = int(input("Enter maximum calories: ")) # calculate the minimum and maximum calories for the sandwich min_sandwich_calories = 2 * ingredients["Bread"] + min(ingredients.values()) * 2 max_sandwich_calories = 2 * ingredients["Bread"] + max(ingredients.values()) * 2 # check if the calorie range is valid if max_calories < min_sandwich_calories: print("Sorry, it is impossible to create a sandwich within the given calorie range.") else: # create the sandwich sandwich = ["Bread", "Bread"] sandwich_calories = 2 * ingredients["Bread"] while sandwich_calories < min_calories: # add random ingredient ingredient = random.choice(list(ingredients.keys())) sandwich.append(ingredient) sandwich_calories += ingredients[ingredient] while sandwich_calories <= max_calories: # add random ingredient ingredient = random.choice(list(ingredients.keys())) # check if the ingredient is the same as the previous one if len(sandwich) >= 3 and ingredient == sandwich[-2]: continue sandwich.append(ingredient) sandwich_calories += ingredients[ingredient] # check if the sandwich is already at the maximum calorie limit if sandwich_calories == max_sandwich_calories: break # add the last slice of bread sandwich.append("Bread") # print the sandwich and its total calories print("Your sandwich:", sandwich) print("Total calories:", sandwich_calories)
最新发布
06-10
以下是转换后的 C# 代码,您可以直接将其复制粘贴到 Visual Studio 中进行编译运行: ``` using System; using System.Collections.Generic; namespace SandwichCalories { class Program { static void Main(string[] args) { // sandwich ingredients and their associated calories Dictionary<string, int> ingredients = new Dictionary<string, int>() { { "Bread", 100 }, { "Ham", 150 }, { "Lettuce", 10 }, { "Tomato", 20 }, { "Mayonnaise", 50 }, { "Cheese", 120 } }; // prompt user for calorie range Console.Write("Enter minimum calories: "); int min_calories = int.Parse(Console.ReadLine()); Console.Write("Enter maximum calories: "); int max_calories = int.Parse(Console.ReadLine()); // calculate the minimum and maximum calories for the sandwich int min_sandwich_calories = 2 * ingredients["Bread"] + ingredients.Values.Min() * 2; int max_sandwich_calories = 2 * ingredients["Bread"] + ingredients.Values.Max() * 2; // check if the calorie range is valid if (max_calories < min_sandwich_calories) { Console.WriteLine("Sorry, it is impossible to create a sandwich within the given calorie range."); } else { // create the sandwich List<string> sandwich = new List<string> { "Bread", "Bread" }; int sandwich_calories = 2 * ingredients["Bread"]; while (sandwich_calories < min_calories) { // add random ingredient string ingredient = ingredients.Keys.ElementAt(new Random().Next(ingredients.Count)); sandwich.Add(ingredient); sandwich_calories += ingredients[ingredient]; } while (sandwich_calories <= max_calories) { // add random ingredient string ingredient = ingredients.Keys.ElementAt(new Random().Next(ingredients.Count)); // check if the ingredient is the same as the previous one if (sandwich.Count >= 3 && ingredient == sandwich[sandwich.Count - 2]) { continue; } sandwich.Add(ingredient); sandwich_calories += ingredients[ingredient]; // check if the sandwich is already at the maximum calorie limit if (sandwich_calories == max_sandwich_calories) { break; } } // add the last slice of bread sandwich.Add("Bread"); // print the sandwich and its total calories Console.WriteLine("Your sandwich: " + string.Join(", ", sandwich)); Console.WriteLine("Total calories: " + sandwich_calories); } } } } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 14
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值