python函数似乎没有正确循环

2024-10-01 11:41:18 发布

您现在位置: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()

从上面我预计会发生什么,如果用户输入的不是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

它应该再问一次问题。你知道吗

当答案确实是“是”或“是”时:

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

^这是正确的。你知道吗

当条件不是“是”或“y”阻止函数再次提问时,我遗漏了什么?你知道吗

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                                 '}'])

谢谢!你知道吗


Tags: orkey函数用户nameinputyoursecret