2017-01-02 39 views
-3

我想了解我怎麼能運行下面的Python代碼有道明白真。我猜想評論會做下面的工作。 現在,這只是工作得很好萬一我輸入「真」或「假」大寫首字母。但我也需要得到同樣的結果輸入1或true時等我需要蟒蛇1時,是的,Y鍵入

# Here we ask the user place an input and assign it to name parameter. 
name = raw_input ("What is your name human?: ") 

# Here we will display the value of name parameter placed by user 
print "Got it! Your name is %r" % name 

# Here we ask the user if he is owning the device. 
# We will assign the answer to computers_owner parameter 
computers_owner = input ("Are you the owner of this machine?: ") 

# In this question's answer we have an if condition. 

# If the answer is True machine will say "Good! I need you" % name 
if computers_owner is True: 
    print "Good! I need you %s" % name 

# If the answer is False machine will say "Call me your master!" % name 
else: 
    print "Call me your master! %s" % name 
+0

使用的raw_input'()'並且像這樣比較:'computers_owner.lower()==「true」' – MYGz

+0

並且像你提到的那樣添加更多條件'computers_owner.lower()=='true'或computers_owner.lower()=='y'或者computers_owner == 1'。你明白了。 –

+1

用戶爲什麼會想到要* *能夠進入'1'的答案是/否的問題嗎?只需用'y'或'n'檢查一個開始的字符串(忽略大小寫)並完成它。 – chepner

回答

3

您可以使用真實情況是這樣的列表:在第二個輸入作爲

name = raw_input ("What is your name human?: ") 
print "Got it! Your name is %r" % name 
computers_owner = raw_input("Are you the owner of this machine?: ") 
if computers_owner in ['1','yea','y','yo','hell yeah','true','True', 'true dat']: 
    print "Good! I need you %s" % name 
else: 
    print "Call me your master! %s" % name 
+0

這正是我想要做的。謝謝你的幫助。 – phantomxxx

+0

@phantomxxx \t如果回答了您的問題,您可以接受以下答案:[Accepting Answer](http://meta.stackexchange.com/a/5235/345277) – MYGz

+0

對不起,新的在這裏,並試圖找出什麼是什麼。我剛接受它。謝謝你的耐心。 – phantomxxx