【发布时间】:2020-05-05 01:15:43
【问题描述】:
我想找到一种方法,通过关联 input() 使用的单词来调用列表中随机选择的值。
目前有两个“报价单”(sadMessages 和 happyMessages)。
所以如果我输入“sad”,程序会从“sad list”中随机选择 如果我输入“快乐”,程序将从“快乐列表”中随机选择 这是它需要的外观。
import random
happy= ["It is during our darkest moments that we must focus to see the light.",
"Tell me and I forget. Teach me and I remember. Involve me and I learn.",
"Do not go where the path may lead, go instead where there is no path and leave a trail.",
"You will face many defeats in life, but never let yourself be defeated.",
"The greatest glory in living lies not in never falling, but in rising every time we fall.",
"In the end, it's not the years in your life that count. It's the life in your years.",
"Never let the fear of striking out keep you from playing the game.",
"Life is either a daring adventure or nothing at all."]
sad= ["Dont cry because it’s over, smile because it happened.",
"Be yourself; everyone else is already taken",
"No one can make you feel inferior without your consent.",
"It’s not who you are that holds you back, its who you think you're not.",
"When you reach the end of your rope, tie a knot in it and hang on."]
print("please choose between happy or sad")#I want to change this to (In one word type how you feel.)
answer = input ()
if answer == "happy":
print("Check this out : " , random.choice(happy+sad))
elif answer == "sad":
print("This might help out : " , random.choice(sad))
【问题讨论】:
-
嗨,我不太清楚你在问什么?另外,尽量减少您的问题和代码(即删除任何不相关的代码)
-
请尽量使问题更简洁,我无法理解。我认为字典可能是您实现目标的更好结构,但我无法理解您的程序。
-
我已对问题进行了更改 - 谢谢。
-
您可以将这两个列表存储在一个带有“sad”和“happy”键的字典中,并将新单词列表添加到该字典中。将这些词表存储在文本文件中并在运行时将它们加载到字典中就很容易了。除此之外,不清楚你在问什么。