2016-10-18 91 views
0

我寫了一個函數,詢問用戶一些信息。在任務結束時,它將提示用戶確定其是否正確。如果是或是,那麼該功能應該繼續。否則它應該重複這些問題。我無法弄清楚,無論爲什麼功能停止的用戶輸入的內容:python函數似乎沒有正確循環

157 def runAllfunctions(): 
158 #  getAccessSecretName() 
159   mcIPNumber() 
160   makeNameMCTag() 
161   makeTfvars() 
162   makeMainTF() 
163   Provisioners() 
164   Resources() 
165   Outputs() 
166 
167 
168 def runTerraform(): 
169   getAccessSecretName() 
170   infoCorrect = raw_input('Is the information above correct? (y or n)') 
171   if infoCorrect.lower() == "yes" or "y": 
172     runAllfunctions() 
173   else: 
174     runTerraform() 
175 
176 runTerraform() 

我希望從上面的情況發生,如果用戶輸入以外的任何其他是或y將重新運行runTerraform()這會再次提示用戶輸入信息,直到它是正確的。一旦它是正確的它會經過和運行的其餘功能

這裏是我所看到的:

如果答案是不肯定或y

You Entered: 
Access Key: asdfds 
Secret Key: asfads 
Your full name is: dsafd dsafdas 

Is the information above correct? (y or n)n 
newnumber = 16 
Your EC2 instance will tagged: 
Name Tag: vlslabs16 
Multicast Tag: vlslabmc, 172.16.0.16 

它應該重新問再次提問。

如果答案是肯定的確實或y:

python terraTFgen.py 

Enter Access Key: asdfdsa 
Enter Secret Key: asdfads 
Enter your name: asdfads 

You Entered: 
Access Key: asdfdsa 
Secret Key: asdfads 
Your full name is: asdfads 

Is the information above correct? (y or n)y 
newnumber = 16 
Your EC2 instance will tagged: 
Name Tag: vlslabs16 
Multicast Tag: vlslabmc, 172.16.0.1 

^這是正確的。

當條件不是「是」或「是」,防止功能再次提問時,我錯過了什麼?

PS這是詢問的情況下,這些問題你是好奇,還是功能,如果這有助於

13 def getAccessSecretName(): 
14   global access_key, secret_key, yourName 
15   access_key = raw_input("Enter Access Key: ") 
16   secret_key = raw_input("Enter Secret Key: ") 
17   yourName = raw_input("Enter your name: ") 
18 
19   print "\n\nYou Entered:" 
20   print "Access Key: %s" % access_key 
21   print "Secret Key: %s" % secret_key 
22   print "Your full name is: %s\n" % yourName 
23   with open (tfVariables,"w") as text_file: 
24     text_file.writelines(['access_key = \"'+ access_key +'\"\nsecret_key = \"'+ se cret_key +'\"\n\n\n', 
25         'amis = {\n', 
26         ' ', 
27         'us-west-1 = '+ usWest1ami +'\n', 
28         ' ', 
29         'us-west-1 = '+ usWest2ami +'\n', 
30         ' ', 
31         '}']) 

謝謝!

+2

'無論什麼或'y''總是如此,因爲字符串''''是真的。平等測試不是由'或'分配的。你需要在[「yes」,「y」]'中使用infoCorrect.lower()==「yes」或infoCorrect.lower()==「y」'或者'infoCorrect.lower()。 – Blckknght

+0

@Blckkngt我想我明白你的意思,但爲什麼不分發?似乎合乎邏輯的「不正確」不適用於這兩種情況? – chowpay

+0

'或'看不到操作員,只有結果的值。所以'「foo」==「bar」或「foo」'相當於'False'或'foo''。然後短路並返回「foo」(這是真的)。 – Blckknght

回答

1

if條件應爲if infoCorrect.lower() == "yes" or infoCorrect.lower() == "y":