是否有Python备用语句

时间:2014-08-09 01:04:23

标签: python

我想知道在Python中是否可以像下面这样的替代语句:if input = bad:print('那不好'),继承我的代码:

print('Hello')
print('What Is Your Name?')
myname = input ()
print('It Is Good To Meet You, ' + myname)
print('How Are You Today')  
emotion = input ()
print('Me Too!') 

如果可以,请帮忙。

4 个答案:

答案 0 :(得分:2)

查看Python的iF语句。在线查看Python的文档,但基本上只是:

if [condition]:
    [statements]
elif [condition]:
    [statements]
else:
    [statements]

答案 1 :(得分:2)

您可能想要使用if / else if / statement。

if myname == "guido":
    print("hello guido")
elif myname == "dmr":
    print("You aren't dennis")
else:
    print("that is a bad input.")

答案 2 :(得分:1)

In [3]: emotion = input('How are you today? ')
How are you today? bad

In [4]: if emotion == 'bad':
   ...:     print("That`s not good")
   ...: else:
   ...:     print("Great!")
   ...:     
That`s not good

实际上,您需要的是:

print('Hello')
print('What Is Your Name?')
myname = input()
print('It Is Good To Meet You, ' + myname)
print('How Are You Today')  
emotion = input()
if emotion == 'bad':
    print("That`s not good")

答案 3 :(得分:1)

是的,有。事实上,它几乎就像你说的那样。它被称为if语句:

emotion = input()

if emotion == bad:
    print ("That's not good!")
else:
    print ('Me too!')