我为GCSE Computing创建了一个配方程序,但我错过了一步

时间:2015-02-24 00:29:46

标签: python python-3.x io python-3.4 file-handling

这是我在这里的第一篇文章,我不太确定如何在课程中实现一段重要的代码。我在Python 3.4上创建了一个非常粗略和基本的配方程序。以下是我遗失的文章。

•程序应要求用户输入人数。

•程序应输出:

•食谱名称

•新人数

•修订后的数量,包含此人数的单位。

我是编程的完全初学者,我们的老师不是很有帮助,只解释了我试图在这个程序中实现的文件处理的基础知识。

我将附上我到目前为止的代码,但我真的很感激一些提示或解释如何将其实现到我的代码中,并最终完成此任务,因为它变得非常令人厌烦。

谢谢!

代码:

如果代码混乱或没有意义,我道歉。我是一个完整的新手。

      #!/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()

编辑:这是新代码,但我仍然无法弄清楚如何允许用户通过输入需要的服务数来增加原始份数,因为默认服务是在文本文件。有谁知道如何做到这一点?我是文件处理的新手,一直在研究,但无济于事。

2 个答案:

答案 0 :(得分:1)

对于人数,您可以通过num_people = input("How many people?")从用户输入中获得与您在代码中获得任何其他输入相似的内容。

你应该看一些关于你的事情。你的start()函数调用它自己。除非你使用递归,否则函数不应该自己调用,它会在堆栈上构建。使用类似

之类的while循环
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"

答案 1 :(得分:0)

您将整个数据存储在文本文件中。这使得存储和检索变得容易,但是更改值真正的。在这些情况下,你通常会使用......好吧,没关系:你asked the same question again and got a useful answer