python生成随机列表最后将列表合成一个_从多个列表中生成随机项的Python代码

我是Python新手,我正试图编写一个代码,通过从列表[characters][traits][themes][locations]和[lines]中随机选择一个条目来生成故事的核心元素。我写的代码如下。在

我有两个问题:

1)目前代码允许我在每个列表中创建新条目,但是当我关闭Python并重新启动程序时,我创建的条目就不见了。如何使新的条目永久地存储在列表中?我需要某种数据库吗?在

2)6的代码。故事盒-我想让程序做的主要事情!-不起作用。关于如何让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.")

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值