为什么try-catch中的异常语句不起作用?

时间:2019-05-25 12:27:51

标签: python python-3.x try-catch

当程序在try-catch语句内部运行,特别是在除外部分中运行时,出现问题。我输入了一个字符串,当在try语句中该字符串不相同时,它将移至除外部分。不会打印出除该部分以外的代码。

def person_feelings(happy_or_sad):
    feelings = happy_or_sad
    return feelings


def person(me, p_smile):
    if me.lower() == 'happy':
        print(f"{p_smile.lower()}")
    else:
        try:
            if me.lower() == 'sad':
                print(f"try to {p_smile.lower()}")
        except:
            print("maybe some other day")


def smile():
    me_smile = 'Smile'
    return me_smile

happy_or_sad = input("happy or sad?")
person(person_feelings(happy_or_sad), smile())

1 个答案:

答案 0 :(得分:-1)

首先,“ try”和“ except”必须从左开始以相等的间距对齐。然后“例外”部分仅在发生异常时才起作用,而在if条件为false时才起作用。

相关问题