В Python как связать слова со списком, а затем случайным образом распечатать значение из списка? - PullRequest
0 голосов
/ 05 мая 2020

Я хочу найти способ вызвать случайно выбранное значение в списке, связав слово, используемое input ().

Есть две "цитаты" списки »(sadMessages и happyMessages) на данный момент.

Итак, если я наберу« грустный », программа случайным образом выберет из« печального списка ». И если я наберу« счастливый », программа случайным образом выберет из «счастливого списка» enter image description here Вот как это должно выглядеть.

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))

1 Ответ

1 голос
/ 16 мая 2020

Надеюсь, это будет вам полезно.

import random
helper= {}
helper['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."]

helper['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."]

answer = input ('How do you feel : ')
print("Check this out : " , random.choice(helper[answer]))
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...