(Python) How can I let the user open a text file and then change the integer / number

I asked a similar question, but to no avail.

I am a beginner student in programming, and I have been taught some basic techniques. Part of the task is to create a recipe program that I basically did; there is only one part that keeps me from finishing.

I must allow the user to call the previously created text file (I made this bit), after which the contents of this file should be displayed for them (I also made this bit), however, the user should be able to recount the portions and, therefore, change the number of ingredients. Therefore, if the user entered: “I want 2 servings,” and the initial amount for 1 serving was 100 g, now it should output 200 g.

It really upsets me, and my teacher expects this work tomorrow. The following is what I should allow the user.

The user should be able to get a recipe and recount the ingredients for a different number of people.

• The program should ask the user to enter the number of people.

• The program should output:

• name of the recipe

• new number of people

• revised quantities with units for this number of people.

I will post my actual code below to show what I have done so far to allow the user to browse and make a new recipe. But there is no bit of corrected values.

I'm sorry if the code is dirty or unorganized, I'm new to this.

The code:

#!/usr/bin/env python

import time

def start():

    while True:

        user_input = input("\nWhat would you like to do? " "\n 1) - Enter N to enter a new recipe. \n 2 - Enter V to view an exisiting recipe, \n 3 - Enter E - to edit a recipe to your liking. \n 4 - Or enter quit to halt the program " "\n ")

        if user_input == "N":
            print("\nOkay, it looks like you want to create a new recipe. Give me a moment..." "\n")
            time.sleep(1.5)
            new_recipe()

        elif user_input == "V":
            print("\nOkay, Let proceed to let you view an existing recipe stored on the computer")
            time.sleep(1.5)
            exist_recipe()

        elif user_input == "E":
            print("\nOkay, it looks like you want to edit a recipe servings. Let proceed ")
            time.sleep(1.5)
            modify_recipe()

        elif user_input == "quit":
            return

        else:
            print("\nThat is not a valid command, please try again with the commands allowed ")


def new_recipe():
    new_recipe = input("Please enter the name of the new recipe you wish to add! ")
    recipe_data = open(new_recipe, 'w')
    ingredients = input("Enter the number of ingredients ")
    servings = input("Enter the servings required for this recipe ")

    for n in range (1,int(ingredients)+1):

        ingredient = input("Enter the name of the ingredient ")
        recipe_data.write("\nIngrendient # " +str(n)+": \n")
        print("\n")
        recipe_data.write(ingredient)
        recipe_data.write("\n")
        quantities = input("Enter the quantity needed for this ingredient ")
        print("\n")
        recipe_data.write(quantities)
        recipe_data.write("\n")
        unit = input("Please enter the unit for this quantity (i.e. g, kg) ")
        recipe_data.write(unit)
        print("\n")

    for n in range (1,int(ingredients)+1):
        steps = input("\nEnter step " + str(n)+ ": ")
        print("\n")
        recipe_data.write("\nStep " +str(n) + " is to: \n")
        recipe_data.write("\n")
        recipe_data.write(steps)
    recipe_data.close()

def exist_recipe():
    choice_exist= input("\nOkay, it looks like you want to view an existing recipe. Please enter the name of the recipe required. ")
    exist_recipe = open(choice_exist, "r+")
    print("\nThis recipe makes " + choice_exist)
    print(exist_recipe.read())
    time.sleep(1)

def modify_recipe():
    choice_exist = input("\nOkay, it looks like you want to modify a recipe. Please enter the name of this recipe ")
    exist_recipe = open(choice_exist, "r+")    
    servrequire = int(input("Please enter how many servings you would like "))


start()

EDIT:

() ( bread.txt) , , .

What would you like to do? 
 1) - Enter N to enter a new recipe. 
 2 - Enter V to view an exisiting recipe, 
 3 - Enter E - to edit a recipe to your liking. 
 4 - Or enter quit to halt the program 
 N

Okay, it looks like you want to create a new recipe. Give me a moment...

Please enter the name of the new recipe you wish to add! bread.txt
Enter the number of ingredients 3
Enter the servings required for this recipe 1
Enter the name of the ingredient flour


Enter the quantity needed for this ingredient 300


Please enter the unit for this quantity (i.e. g, kg) g


Enter the name of the ingredient salt


Enter the quantity needed for this ingredient 50


Please enter the unit for this quantity (i.e. g, kg) g


Enter the name of the ingredient water


Enter the quantity needed for this ingredient 1


Please enter the unit for this quantity (i.e. g, kg) l



Enter step 1: pour all ingredients into a bowl



Enter step 2: mix together



Enter step 3: put in a bread tin and bake

What would you like to do? 
 1) - Enter N to enter a new recipe. 
 2 - Enter V to view an exisiting recipe, 
 3 - Enter E - to edit a recipe to your liking. 
 4 - Or enter quit to halt the program 
 V

Okay, Let proceed to let you view an existing recipe stored on the computer

Okay, it looks like you want to view an existing recipe. Please enter the name of the recipe required. bread.txt

This recipe makes bread.txt

Ingrendient # 1: 
flour
300
g
Ingrendient # 2: 
salt
50
g
Ingrendient # 3: 
water
1
l
Step 1 is to: 

pour all ingredients into a bowl
Step 2 is to: 

mix together
Step 3 is to: 

put in a bread tin and bake

, V:

bread.txt

Ingrendient # 1: 
flour
300
g
Ingrendient # 2: 
salt
50
g
Ingrendient # 3: 
water
1
l
Step 1 is to: 

pour all ingredients into a bowl
Step 2 is to: 

mix together
Step 3 is to: 

put in a bread tin and bake

.

+4
1

, :

Ingrendient # N: 
{INGREDIENT}
{AMOUNT}
{METRIC}
...
(After N ingredients...)
Step N: 
{INSTRUCTIONS}

, , , - .

with open(path_to_recipe) as infile:
    ingredients = []
    while True:
        try:
            sentinel = next(infile) # skip a line
            if sentinel.startswith("Step"):
                # we're past the ingredients, so
                break
            name = next(infile)
            amount = next(infile)
            metric = next(infile)
        except StopIteration:
            # you've reached the end of the file
            break
        ingredients.append({'name':name, 'amount':float(amount), 'metric':metric})
        # use a dictionary for easier access

with, ingredients , :

for ingredient in ingredients:
    scaled_volume = ingredient['amount'] * scale # double portions? etc...
    print(scaled_volume, ingredient['metric'], " ", ingredient['name'])
# # RESULT IS:
300g flour
50g salt
1l water

, !

+2

All Articles