Python 有人能告诉我,对于一个根据情绪给出qoutes的函数,这个代码有什么问题吗?

Python 有人能告诉我,对于一个根据情绪给出qoutes的函数,这个代码有什么问题吗?,python,Python,我想编写一个函数,根据你的情绪给你一个激励性的引用。这是我的代码: import secrets myLst = ["Cheer up. Although life may not be easy now, in the end it’s all worth it. Everything will get better in time. So SMILE!", "the sun will rise and we will try again", "No one is in control

我想编写一个函数,根据你的情绪给你一个激励性的引用。这是我的代码:

import secrets

myLst = ["Cheer up. Although life may not be easy now, in the end it’s all 
worth it. Everything will get better in time. So SMILE!", "the sun will rise 
and we will try again", "No one is in control of your happiness but you; 
therefore, you have the power to change anything about yourself or your life 
that you want to change."]
myLst2 = ["We Generate Fears While We Sit. We Overcome Them By Action", "You 
Are Never Too Old To Set Another Goal Or To Dream A New Dream", "Of course 
motivation is not permanent. But then, neither is bathing; but it is 
something you should do on a regular basis."]
myLst3 = ["Speak when you are angry and you will make the best speech you 
will ever regret", "If you spend your time hoping someone will suffer the 
consequences for what they did to your heart, then you're allowing them to 
hurt you a second time in your mind", "When angry, count four. When very 
angry, swear"]
myLst4 = ["Let us be grateful to people who make us happy", "I have only two 
kinds of days: happy and hysterically happy", "Be happy. It’s one way of 
being wise"]



 def motivationalQuote():    
        userInput = input ("What is your mood? Sad, unmotivated, angry or 
        happy?")
            if sad in userInput
              print(secrets.choice(myLst))
        else:
            if unmotivated in userInput
                print(secrets.choice(myLst2))
            else:
                if angry in userInput
                    print(secrets.choice(myLst3))
                else:
                    print(secrets.choice(myLst4))

有人能帮我吗?我是一个abslute初学者,我不知道如何修复它

为了更具python风格,我会把引号放在字典里:

import random

sad_quotes = [
    "Cheer up. Although life may not be easy now, in the end it’s all worth it. Everything will get better in time. So SMILE!",
    "the sun will rise and we will try again",
    "No one is in control of your happiness but you; therefore, you have the power to change anything about yourself or your life that you want to change.",
]
unmotivated_quotes = [
    "We Generate Fears While We Sit. We Overcome Them By Action",
    "You Are Never Too Old To Set Another Goal Or To Dream A New Dream",
    "Of course motivation is not permanent. But then, neither is bathing; but it is something you should do on a regular basis.",
]
angry_quotes = [
    "Speak when you are angry and you will make the best speech you will ever regret",
    "If you spend your time hoping someone will suffer the consequences for what they did to your heart, then you're allowing them to hurt you a second time in your mind",
    "When angry, count four. When very angry, swear",
]
happy_quotes = [
    "Let us be grateful to people who make us happy",
    "I have only two kinds of days: happy and hysterically happy",
    "Be happy. It’s one way of being wise",
]

quotes = {
    'sad': sad_quotes,
    'unmotivated': unmotivated_quotes,
    'angry': angry_quotes,
    'happy': happy_quotes,
}


def motivationalQuote():
    user_mood = input("What is your mood? Sad, unmotivated, angry or happy?")
    if user_mood in quotes:
        quote = random.choice(quotes[user_mood])
        print(quote)
    else:
        print('I have no quotes for a ' + user_mood + ' mood.')
有几件事:

if语句需要a:在其末尾 sad、无动机等被解释为变量。我想你的意思是如果userInput中的'sad' 这实际上并没有破坏您的代码,但会将其清理干净——与其在else中嵌套更多if语句,不如使用elif 改进-您的代码当前假定任何不包括悲伤、无动机或愤怒的答案都必须是快乐的。为什么不显式地处理happy并添加一个额外的案例,让用户知道您不理解

import secrets
myLst = ["Cheer up. Although life may not be easy now, in the end it’s all worth it. Everything will get better in time. So SMILE!", "the sun will rise and we will try again", "No one is in control of your happiness but you; therefore, you have the power to change anything about yourself or your life that you want to change."]
myLst2 = ["We Generate Fears While We Sit. We Overcome Them By Action", "You Are Never Too Old To Set Another Goal Or To Dream A New Dream", "Of course motivation is not permanent. But then, neither is bathing; but it is something you should do on a regular basis."]
myLst3 = ["Speak when you are angry and you will make the best speech you will ever regret", "If you spend your time hoping someone will suffer the consequences for what they did to your heart, then you're allowing them to hurt you a second time in your mind", "When angry, count four. When very angry, swear"]
myLst4 = ["Let us be grateful to people who make us happy", "I have only two kinds of days: happy and hysterically happy", "Be happy. It’s one way of being wise"]


def motivationalQuote():    
        userInput = input ("What is your mood? Sad, unmotivated, angry or happy?")
        if 'sad' in userInput:
              print(secrets.choice(myLst))
        elif 'unmotivated' in userInput:
              print(secrets.choice(myLst2))
        elif 'angry' in userInput:
              print(secrets.choice(myLst3))
        elif 'happy' in userInput
              print(secrets.choice(myLst4))
        else:
              print('I cannot tell how you are feeling!')
密码 变化概述 您不需要使用if/else的层;只需使用一个elif语句。您在一些地方忘记了冒号来引入新的逻辑块,而且您也没有将要搜索的字符串放在引号中。您的一些变量/函数名是camelCase,因此我根据PEP8将它们更改为下划线大小写。在我看来,与其创建没有输入的函数,不如定义一个名为mood的变量,并将其定义为函数的参数,然后调用下面的函数。你的名单名字不清楚,所以我改了名字


有更有效的方法可以做到这一点,但我保留了您的结构,因为我认为您可能会更清楚。

您希望修复什么?当前发生了什么?您期望或希望发生什么?如果用户输入中出现sad。sad是在其他地方声明的吗?当我尝试使用代码时,在用户输入行中的if sad中出现语法错误,您缺少几个冒号。使用带有语法突出显示的编辑器/IDE,例如PyCharm的社区版。
import random
sad = ["Cheer up. Although life may not be easy now, in the end it’s all 
worth it. Everything will get better in time. So SMILE!", "the sun will rise 
and we will try again", "No one is in control of your happiness but you; 
therefore, you have the power to change anything about yourself or your life 
that you want to change."]
unmotivated = ["We Generate Fears While We Sit. We Overcome Them By Action", "You 
Are Never Too Old To Set Another Goal Or To Dream A New Dream", "Of course 
motivation is not permanent. But then, neither is bathing; but it is 
something you should do on a regular basis."]
angry = ["Speak when you are angry and you will make the best speech you 
will ever regret", "If you spend your time hoping someone will suffer the 
consequences for what they did to your heart, then you're allowing them to 
hurt you a second time in your mind", "When angry, count four. When very 
angry, swear"]
happy = ["Let us be grateful to people who make us happy", "I have only two 
kinds of days: happy and hysterically happy", "Be happy. It’s one way of 
being wise"]

mood = input("What is your mood? Sad, unmotivated, angry or happy? ")

def motivational_quote(mood):
    if 'sad' in mood:
        print(random.choice(sad))
    elif 'unmotivated' in mood:
        print(random.choice(unmotivated))
    elif 'angry' in mood:
        print(random.choice(angry))
    else:
        print(random.choice(happy))
motivational_quote(mood)