I want to write a function that takes a string, finds the tsp or tbsp format, and converts that to gram.
Then, I store this information in c and have to insert it behind the tbsp/tsp word in the string. Since strings are immutable I was thinking of converting it to a list first, but now I am a bit stuck.
Anyone has advice on how to do this? :)
Examples:
input output
"2 tbsp of butter" --> "2 tbsp (30g) of butter"
"1/2 tbsp of oregano" --> "1/2 tbsp (8g) of oregano"
"1/2 tsp of salt" --> "1/2 tbsp (3g) of salt"
def convert_recipe(recipe):
c = ''
for i in recipe: # save the digit
if i.isdigit():
c += i
if 'tsp' in recipe: # convert tsp to gram
c = int(c) * 5
elif 'tbsp' in recipe: # convert tbsp to gram
c = int(c) * 15
# now we have c. Insert (c) behind tsp / tbsp in string
recipe = recipe.split()
print(recipe)
convert_recipe("2 tbsp of butter")