3

This is my first post here and I am quite unsure on how to implement a vital piece of code in my coursework. I have created a very crude and basic recipe program on Python 3.4. Below is the piece I am missing.

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

• The program should output:

• the recipe name

• the new number of people

• the revised quantities with units for this number of people.

I am a complete beginner to programming and our teacher hasn't been very helpful and only explained the basics of file handling which I have attempted to implement into this program.

I will attach the code I have so far, but I would really appreciate some tips or an explanation on how I could implement this into my code and finally finish this task off as it is becoming very irksome.

Thank you!

Code:

I apologise if the code is cluttered or doesn't make sense. I am a complete novice.

      #!/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's 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's servings. Let's 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")

    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("\nOkaym it looks like you want to modify a recipe. Please enter the name of this recipe ")
    Exist_Recipe = open(Choice_Exist, "r+")
    time.sleep(2)
    ServRequire = int(input("Please enter how many servings you would like "))


start()

EDIT: This is the new code, however I can still not figure out how to allow a user to multiply the original servings by entering how many servings are required, as the default servings are in a text file. Does anyone know how this can be done? I am new to file-handling and have been researching constantly but to no avail.

  • 1
    Hi @SpartanSK117, nicely laid out question. One small stylistic tip (not related to your specific question - variables in python (e.g. `New_Recipe`, `Ingredients`) should be all in lower case (e.g. `new_recipe`, `ingredients`). This is as specified in the python style guide: https://www.python.org/dev/peps/pep-0008/. – robjohncox Feb 25 '15 at 16:23
  • Thank you, surprisingly, my teacher is the one who said we should add a capital at the beginning of variables. I will change this now thanks! Also, do you have any suggestions on how I can rectify my problem? Thanks in advance. – SpartanSK117 Feb 25 '15 at 21:08
  • Possible duplicate of [(Python) How can I let a user open a text file and then change an integer / number](http://stackoverflow.com/questions/28886107/python-how-can-i-let-a-user-open-a-text-file-and-then-change-an-integer-numb) – serv-inc Apr 06 '16 at 09:42
  • Currently you don't save `Servings` to the file in `new_recipe`. If you want to modify the quantities displayed for a different number of servings, you'll need this. Secondly, when you display an existing recipe, you just print the file - this is fine, but to modify the quantities you need to reverse what `new_recipe` did to write the file, so you get the values in variables you can modify before printing. – Useless Apr 06 '16 at 09:51

2 Answers2

1

For the number of people, you can get that from user input similar to how you got any other input in your code, with num_people = input("How many people?").

Something a little more concerning you should look at. Your start() function calls its self. Unless you are using recursion, functions should not call themselves, it will build up on the stack. Use a while loop with something like

while ( 1 ):

    userinput = input("what would you like to do?")

    if( userinput == "n"):
            #new recipe

    ....

    if (user input == "quit"):
            sys.exit(1) #this will halt the program

    else:
          print "not valid input"
OrangePot
  • 1,053
  • 2
  • 14
  • 38
  • of coarse he would have to run for a long time to hit the recursion limit but yes good answwer – Joran Beasley Feb 24 '15 at 00:52
  • 1
    Thank you, I implemented something of this sort into my program. I've decided to re-write it to neaten it up and I've split the create a recipe, view a recipe and modify a recipe into three functions. I'll post back here when I 've fixed it (well most of it). – SpartanSK117 Feb 24 '15 at 23:04
0

You stored the whole data in a text file. This makes storage and retrieval easy, but makes changing values real hard. In these cases, you usually use ... well nevermind: you asked the same question again and got a useful answer.

Community
  • 1
  • 1
serv-inc
  • 35,772
  • 9
  • 166
  • 188