我写了一个函数,询问用户一些信息。在任务结束时,它将提示用户确认其是否正确。如果是或否则该功能应继续。如果不是,它应该重复这些问题。我无法弄清楚为什么函数会停止,无论用户输入什么:
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()
我期望从上面发生的事情,如果用户输入除yes或y以外的任何内容,它将重新运行runTerraform(),这将再次提示用户输入信息,直到它正确为止。一旦它正确,它将通过并运行其余的功能
这就是我所看到的:
当答案不是是或是
时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 '}'])
谢谢!
答案 0 :(得分:1)
if条件应为if infoCorrect.lower() == "yes" or infoCorrect.lower() == "y":
。