Python If/Else Statements



说到python,我有点新手。。。

然而,我无法使这段代码正常工作。我花了很长时间在各种各样的书和问题网站上试图找出问题所在,所以我来到了这里。

我的pythonidle(python GUI)(我正在使用的软件)每当我使用Else语句时,都会不断出现语法错误。。。

a = str
print("What is Your name")
a = input()
b = str
print("Oh Hello " + a)
print("How Are You Feeling")
b = input()
if b == "Happy":
    print("Glad to hear that":
          else:
          print("Oh im sorry to hear that")

很抱歉,这是一次与电脑的对话,但我从来没有选择主题,所以我忍不住…

请帮助

缩进在Python中很重要。else必须缩进与其配对的if完全相同的数量

a = str
print("What is Your name")
a = input()
b = str
print("Oh Hello " + a)
print("How Are You Feeling")
b = input()
if b == "Happy":
    print("Glad to hear that")   # Added the missing ) here as well
else:
    print("Oh im sorry to hear that")

else语句需要与其对应的if具有相同的缩进。打印结束时还缺少一个),而打印结束时不需要该:

if b == "Happy":
    print("Glad to hear that")  # <== missing that last ), you don't need that :
else:  # <== indentation was wrong here
    print("Oh im sorry to hear that")

最新更新