我是编程的新手,我有一份菜谱。该食谱应该根据人数来更改配料。
recipeList = [["Egg",3,"st"], #list of the ingredients
["Sugar",3,"dl"], #with 4 people as base
["Vanilla sugar",2,"tsp"],
["Baking powder",2,"tsp"],
["Flour",3,"dl"],
["Butter",75,"g"],
["Water",1,"dl"]]
print("How many people are going to eat the cake?")
x = int(input())#input for user
print("Recipe for a sponge cake for", x, "people")
print("| Ingredients | Amount") #a list for ingredients and amount
for item in recipeList:
print("|",item[0]," "*(13-len(item[0])),"|", #visual design for the list
(item[1]*x/4), #amount * x/4, as recipe is based on 4 people
item[2]," "*(3-len(item[2])-len(str(item[1]))),
)
我的问题是,使用此代码和列表类型,我可以将鸡蛋数量打印为整数吗?我不希望蛋印上有小数。分配工作正常,鸡蛋结果为0
只需这样做:
recipeList = [["Egg", 3, "st"], # list of the ingredients
["Sugar", 3, "dl"], # with 4 people as base
["Vanilla sugar", 2, "tsp"],
["Baking powder", 2, "tsp"],
["Flour", 3, "dl"],
["Butter", 75, "g"],
["Water", 1, "dl"]]
print("How many people are going to eat the cake?")
x = int(input()) # input for user
print("Recipe for a sponge cake for", x, "people")
print("| Ingredients | Amount") # a list for ingredients and amount
for item in recipeList:
print("|", item[0], " " * (13 - len(item[0])), "|", # visual design for the list
int(item[1] * x / 4) if item[0] == 'Egg' else (item[1] * x / 4), # amount * x/4, as recipe is based on 4 people
item[2], " " * (3 - len(item[2]) - len(str(item[1]))),
)
有条件地在鸡蛋上打印,如果该项目是鸡蛋,则只是int(item [1] * x / 4)