2

I'm new to Python and am trying to write a code that will generate the core elements of a story by choosing one random entry from the lists [characters] [traits] [themes] [locations] and [lines]. The code I have written is below.

I have 2 questions:

1) At the moment the code will allow me to create new entries in each list but when I close down Python and restart the program the entries I created are gone. How can I make it so that new entries are permanently stored in the lists? Do I need some kind of database?

2) The code for 6. Storybox - the main thing I want the program to do! - is not working. Any suggestions as to how I can get Python to print a randomly selected entry from each list?

Thanks in advance for your help.

#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.")
1
  • 1) yes, you need some sort of persistent storage. A database would work, as would a scheme of writing to flat files (csv or pickle, etc). 2) what is not working about it? Commented Mar 2, 2013 at 20:19

1 Answer 1

1

1) Yes, you'll need to store the data somehow, either in a text file or in a database. The database would be overkill at this point, so I'd recommend something like json.

2) deleted randint solution - random.choice is definitely better

Sign up to request clarification or add additional context in comments.

4 Comments

He's already random.choice, which is far better than your manual approach using randint.
Ah, I didn't scroll down to see the rest of his code. I hadn't seen random.choice before, thanks.
Thanks for your answers, json looks useful (although will take me a while to work out how to use it). @tcaswell: the problem is that when I enter 6 an error message comes up: Choice: 6 Your storybox is.... Traceback (most recent call last): File "C:\Python31\Storybox", line 69, in <module> storyboxcharacter = random.choice(characters) NameError: name 'random' is not defined >>>
You have to import the random module by including the line import random at the start of your script.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.