我想知道是否有人能解决这个问题。 我不断地出现错误,我的程序无法相应地为OCR GCSE计算的配方任务工作。我已经设法在一个外部文本文件中创建添加配方,并搜索现有配方。在
我遇到的问题是重新计算新的数量(所有的数字数据)为不同的人数的食谱。我使用的是pythonversion3。在
我的修正程序部分如下。(我默认了一个人的所有配方),这样更容易重新计算新的数量。在
我的代码如下。**
我提前感谢你的帮助。在
import os
def modify():
#create a boolean variable to use as a flag
found = False
#Get the search value and the new recipe information.
search = input ("Enter a recipe name to search for: ")
new_number_of_people =input("Enter the new number of people to serve (default is 1):")
#open the original recipeList.txt file.
recipeList_file=open("recipeList.txt", "r")
#open the temporary file.
temp_file = open("temp.txt", "w")
#Read the first record's recipe name field
recipe = recipeList_file.readline()
#Read the rest of the file.
while recipe != "":
#Read the recipe item,quantity, units and number of people.
ingredient1 = str(recipeList_file.readline())
quantity1 =float(recipeList_file.readline())
units1 = str (recipeList_file.readline())
ingredient2 = str(recipeList_file.readline())
quantity2 =float(recipeList_file.readline())
units2 = str (recipeList_file.readline())
ingredient3 = str(recipeList_file.readline())
quantity3 =float(recipeList_file.readline())
units3 = str (recipeList_file.readline())
number_of_people = float(recipeList_file.readline())
recipe = recipe.rstrip("\n")
#write a new record with the temp file
if recipe == search:
#write the modified record to the temp file.
temp_file.write(recipe + "\n")
temp_file.write(ingredient1+ "\n")
temp_file.write((quantity1*input(new_number_of_people)) + "\n")
temp_file.write(units1 + "\n")
temp_file.write(ingredient2+ "\n")
temp_file.write((quantity2*input(new_number_of_people)) + "\n")
temp_file.write(units2 + "\n")
temp_file.write(ingredient3+ "\n")
temp_file.write((quantity3*input(new_number_of_people)) + "\n")
temp_file.write(units3 + "\n")
temp_file.write((new_number_of_people) + "\n")
#Set the found flag to True.
found = True
else:
#write the original record to the temp file.
#write the modified record to the temp file.
temp_file.write(recipe + "\n")
temp_file.write(ingredient1+ "\n")
temp_file.write((quantity1*input(new_number_of_people)) + "\n")
temp_file.write(units1 + "\n")
temp_file.write(ingredient2+ "\n")
temp_file.write((quantity2*input(new_number_of_people)) + "\n")
temp_file.write(units2 + "\n")
temp_file.write(ingredient3+ "\n")
temp_file.write((quantity3*input(new_number_of_people)) + "\n")
temp_file.write(units3 + "\n")
temp_file.write((new_number_of_people) + "\n")
#Read the next recipe
ingredient1 = str(recipeList_file.readline())
quantity1 = float(recipeList_file.readline())
units1 = str (recipeList_file.readline())
ingredient2 = str(recipeList_file.readline())
quantity2 = float(recipeList_file.readline())
units2 = str (recipeList_file.readline())
ingredient3 = str(recipeList_file.readline())
quantity3 = float(recipeList_file.readline())
units3 = str (recipeList_file.readline())
number_of_people = float(recipeList_file.readline())
#Close the Recipe file and the temporary file.
recipeList_file.close()
temp_file.close()
#Delete the original recipeList.txt file.
os.remove ("recipeList.txt")
#Rename the temporary file.
os.rename("temp.txt", "recipeList.txt")
#if the search was not found in the file display message
if found:
print ("The file has been updated.")
else:
print ("That recipe was not found in the file")
#call the main function.
modify()
在收件人列表.txt格式:
^{pr2}$
输入文件的格式不是最好的。我将更好的每一个配方在一行和每一个数据用分隔符(逗号),一个csv格式。在
但由于输入是以这种格式给出的。。。在
首先,是一个好习惯,为每一项任务做一个功能。因此,我认为有三个任务:
question()
函数。说明:向用户请求输入数据,我以元组形式返回。在
modify()
函数。说明:打开文件输入后,我读取()所有文件,并使用“\n”分隔符拆分每一行。这将生成一个包含文件每行内容的列表。在
由于配方的名称在文件中是唯一的,并且我想在调用函数时访问已确定的配方,我认为最好使用字典,其中 配方是键,其他数据(我把它放在一个列表中)是值。对, dict的值可以是Python中的任何对象,因此我使用列表。我定义了一个空白dict来添加配方。在
一个配方有12个数据(包括末尾的空行),因此使用
zip(*[iter(recipeList)]*12)
我创建了一个由12个元素组成的元组,可以用for
语句进行迭代。在元组的0索引是我用作键的配方的名称。元组的其余部分 (使用从第二个元素到末尾的片段
[1:]
)在列表中转换的是字典对的值。为什么要单子?因为元组在Python中是不可变的。在然后我迭代列表(从第二个元素到第三个元素;数量)按食客的数量用乘积更新值。最后,我用人数更新了第十项。在
此函数返回更新字典。在
save()
函数。 试着自己编写代码。您只需迭代字典(嵌套一个列表)并在一行中将每个数据写入配方的新文件。(别忘了写下每个字典对的键,就是菜谱的名字)。在相关问题 更多 >
编程相关推荐