-1

こんにちは、必要に応じて再読み込みできるテキスト ドキュメントにいくつかのファイルを保存するプログラムに取り組んでいます。以下はコードの冒頭ですが、実行すると、テキスト ファイルの名前として定義したと思っていたときに、「recipe_title が定義されていません」というトレース バック エラーが表示されます。私が間違ったことを教えてください。

import sys

opt=0

def choice1():
    print("WORKED")
def choice2():
    Recipe_Name = input("Please enter a recipe name: ")
    Recipe_List = open(recipe_title.txt,"w")
    Recipe_List.write(recipe_title+"\n")

def ingredient_input_loop(recipe_title, ):
        Recipefile = open(recipe_title,"w")
        if(ingredient== "end" or "End" or "END" or "EnD" or "eNd" or "enD" or "ENd" or "eND"):
            Recipe.write(recipe_title)
4

1 に答える 1

1

レシピ_タイトル.txt はファイル名であり、変数ではありません。したがって、引用符を追加する必要があります

Recipe_List = open('recipe_title.txt',"w")

または、recipe_title が実際に変数である場合:

Recipe_List = open('{}.txt'.format(recipe_title),"w") # now you can open brocolli.txt for example

コードに関する一般的なフィードバック:

  • 変数名には大文字を使用しないでください。これは、クラス名にのみ使用する必要があります。
  • 「end」のすべての組み合わせについて書き込み可能かどうかを確認するif ingredient.lower() == "end":
于 2015-03-17T14:23:25.747 に答える