Python If / Else语句

时间:2013-11-25 19:52:25

标签: python

对于python来说,我有点新手......

然而,我无法让这段代码工作。我花了很长时间试图弄清楚出了什么问题,在各种书籍和问题网站上,所以我来到这里。

我的python 空闲(Python GUI) (我正在使用的软件)当我使用Else语句时,仍然会出现语法错误... < / p>

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

很抱歉这是与计算机的对话,但我从来没有选择这个主题所以我无法帮助它......

请帮忙

2 个答案:

答案 0 :(得分:7)

缩进在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")

答案 1 :(得分:2)

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