Python I';我已经为GCSE计算创建了一个配方程序,但我错过了一个步骤
这是我在这里的第一篇文章,我不确定如何在我的课程中实现一段重要的代码。我在Python3.4上创建了一个非常粗糙和基本的配方程序。下面是我丢失的那块 •程序应要求用户输入人数 •程序应输出: •配方名称 •新的人数 •修订后的数量,以该人数为单位 我是一个完全的编程初学者,我们的老师并没有提供太多帮助,只是解释了我试图在这个程序中实现的文件处理的基础知识 我将附上到目前为止我所拥有的代码,但我真的希望能有一些提示或解释,告诉我如何在我的代码中实现这一点,并最终完成这项任务,因为它变得非常烦人 谢谢大家! 代码: 如果代码混乱或没有意义,我道歉。我完全是个新手Python I';我已经为GCSE计算创建了一个配方程序,但我错过了一个步骤,python,python-3.x,io,python-3.4,file-handling,Python,Python 3.x,Io,Python 3.4,File Handling,这是我在这里的第一篇文章,我不确定如何在我的课程中实现一段重要的代码。我在Python3.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()
编辑:这是新代码,但我仍然无法通过输入所需的份数来允许用户乘以原始份数,因为默认份数在文本文件中。有人知道如何做到这一点吗?我是新的文件处理,一直在研究不断,但没有任何成效 对于人数,您可以从用户输入中获得,类似于您在代码中获得任何其他输入的方式,使用
num\u people=input(“多少人?”)
一些你应该关注的事情。您的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"
您将整个数据存储在一个文本文件中。这使存储和检索变得容易,但使更改值变得非常困难。在这些情况下,您通常使用。。。好吧,别担心:你。当然,他必须运行很长时间才能达到递归极限,但很好的回答。谢谢你,我在我的程序中实现了类似的东西。我决定重新编写它来整理它,并将创建配方、查看配方和修改配方拆分为三个函数。当我把它修好后(大部分都修好了),我会发回这里。你好@SpartanSK117,这个问题布置得很好。一个小的风格提示(与您的特定问题无关-python中的变量(例如
New_Recipe
,components
)应该全部小写(例如New_Recipe
,components
)。这是在python样式指南中指定的:。谢谢,令人惊讶的是,我的老师说我们应该在变量开头添加大写。我现在就更改它。谢谢!另外,你对我如何纠正我的问题有什么建议吗?提前谢了。可能是重复的,目前你不将服务
保存到new\u recipe
中的文件。如果要修改为不同份数显示的数量,则需要此文件。其次,当显示现有配方时,您只需打印文件-这很好,但要修改数量,则需要反转new\u recipe
编写文件时所做的操作,以便获得值在变量中,您可以在打印前修改。这是用python 3标记的,因此没有原始输入。@matsjoyce:谢谢,这是正确的。