Create a simple console application allowing users to "make a sandwich" within a set caloric range. A list of available sandwich ingredients, along with their associated calories, is provided in the starter code. A user should be prompted to enter the minimum and maximum calories. The program will respond by creating and outputting a sandwich within the given calorie range. If the user enters a calorie range that makes it impossible to create a sandwich, the program should respond appropriately. In addition to the calorie range restriction, the program should also consider these requirements: Every sandwich must have exactly two slices of bread at the start and end of the sandwich. You may have multiple copies of ingredients, but they may not be adjacent. Example: {"Bread", "Ham", "Ham", "Lettuce", "Bread"} is NOT okay. Example: {"Bread", "Ham", "Lettuce", "Ham", "Bread"} IS okay. Every sandwich should use at least the minimum number of calories within the given range. Every sandwich should use, at most, the maximum number of calories within the given range without going over.
时间: 2024-03-22 22:39:16 浏览: 14
Sure! Here's a sample code in Python that can create a sandwich within a given calorie range:
```python
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)
```
This program first prompts the user for the minimum and maximum calorie range. It then calculates the minimum and maximum calories for the sandwich based on the ingredients and the requirement of having two slices of bread at the start and end of the sandwich.
If the maximum calorie range is less than the minimum calories required for the sandwich, the program will print an error message. Otherwise, it will create the sandwich by randomly selecting ingredients that meet the calorie range restriction and the requirement of not having adjacent identical ingredients.
Finally, the program prints the sandwich and its total calories.