0

I am new to programming and I have this assignment with a recipe. The recipe is supposed to change the ingredients based on the amount of people.

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]))), 
          )

My question is, with this code and type of list, is it possible for me to print the amount of eggs as an integer? I do not want decimals on the egg-print. The assignment is okay with a 0 as result on egg

koras
  • 1
  • 1
  • Does this answer your question? [Python integer division yields float](https://stackoverflow.com/questions/1282945/python-integer-division-yields-float) – quamrana Apr 10 '20 at 11:32
  • Use `int` functoin it will round down (for positive numbers as in this case) - int(0.1) = 0 – Tom Ron Apr 10 '20 at 11:36
  • Not really, if i put an int on the whole print all of the items will be rounded. I need the others to stay as float and only the egg to be rounded. The code could very well be poorly written and incorrect. But that is kind of my question, if it would be possible to do it with this code or if i need to rewrite it all. – koras Apr 10 '20 at 11:43

2 Answers2

0

Just do this :

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]))),
              )

Conditional printing on the egg, if the item is egg just int(item[1]*x/4)

Abhishek Kulkarni
  • 1,747
  • 1
  • 6
  • 8
0

You can put one if-condition on Egg while printing. There is one more thing that you can improve in this code. You can use str.format() to print your text in more readable. use alignment specifiers to change the alignment.

Here is your improved code.

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 \n")

print("| {:<13} | {:<4}".format("Ingredients","Amount"))                    #a list for ingredients and amount

for item in recipeList:
    if item[0] == 'Egg':
        print('| {:<13} | {:>3} {:<2}'.format(item[0], int(item[1]*x/4), item[2]))
    else:
        print('| {:<13} | {:>4} {:<2}'.format(item[0], item[1]*x/4, item[2]))

Here is output for a sample run

How many people are going to eat the cake?
4
Recipe for a sponge cake for 4 people 

| Ingredients   | Amount
| Egg           |   3 st
| Sugar         |  3.0 dl
| Vanilla sugar |  2.0 tsp
| Baking powder |  2.0 tsp
| Flour         |  3.0 dl
| Butter        | 75.0 g 
| Water         |  1.0 dl
FAHAD SIDDIQUI
  • 631
  • 4
  • 22