2

私はPythonを初めて使用し、リスト[キャラクター][特性][テーマ][場所]と[行]からランダムなエントリを1つ選択して、ストーリーのコア要素を生成するコードを作成しようとしています。私が書いたコードは以下の通りです。

2つの質問があります:

1)現時点では、コードを使用して各リストに新しいエントリを作成できますが、Pythonを閉じてプログラムを再起動すると、作成したエントリは失われます。新しいエントリがリストに永続的に保存されるようにするにはどうすればよいですか?ある種のデータベースが必要ですか?

2)6. Storyboxのコード-プログラムに実行させたい主なこと!- 動かない。Pythonに各リストからランダムに選択されたエントリを印刷させる方法に関する提案はありますか?

よろしくお願いします。

#Storybox program
#Generates a random selection of my story ideas


characters = []
traits = []
locations = []
themes = []
lines = []

choice = None

while choice != "0":

    print(
     """

Storybox:

0 - Exit
1 - Add character
2 - Add trait
3 - Add location
4 - Add theme
5 - Add line
6 - Generate Story
7 - View all entries

"""
)

    choice = input("Choice: ")
    print()


#exit
    if choice == "0":
        print("Goodbye")

#add a character
    elif choice == "1":
        character = input("Enter character: ")
        characters.append(character)

#add a trait
    elif choice == "2":
        trait = input("Enter character trait: ")
        traits.append(trait)

#add a location
    elif choice == "3":
        location = input("Enter location: ")
        locations.append(location)

#add a theme
    elif choice == "4":
        theme = input("Enter theme: ")
        themes.append(theme)

#add good lines
    elif choice == "5":
        line = input("Enter good line: ")
        lines.append(line)


#Generate storybox
    elif choice == "6":
        print("Your storybox is....")
        storyboxcharacter = random.choice(characters)
        print(storyboxcharacter)
        storyboxtrait = random.choice(traits)
        print(storyboxtrait)
        storyboxtheme = random.choice(themes)
        print(storyboxtheme)
        storyboxlocation = random.choice(locations)
        print(storyboxlocation)
        storyboxline = random.choice(lines)
        print(storyboxline)


#Display all entries so far
    elif choice == "7":
        print("Characters:")
        for character in characters:
            print(character)

        print("Traits:")
        for trait in traits:
            print(trait)

        print("Themes:")
        for theme in themes:
            print(theme)

        print("Locations:")
        for location in locations:
            print(location)

        print("Good lines:")
        for line in lines:
            print(line)

input("\n\nPress the enter key to exit.")
4

1 に答える 1

1

1)はい、テキストファイルまたはデータベースのいずれかにデータを保存する必要があります。この時点ではデータベースが過剰になるので、jsonのようなものをお勧めします。

2)削除されたrandintソリューション-random.choiceは間違いなく優れています

于 2013-03-02T20:21:03.943 に答える