0

I wrote a function that asks the user some information. At the end of the task it will prompt the user to make sure if its correct. If yes or y then the function should continue. If else it should repeat the questions. I cant figure out why the function stops regardless of what the user enters:

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()

What I expect to happen from the above, if the user enters anything other than yes or y it will re-run runTerraform(), which prompts the user for the information again until it is correct. Once it is correct it will go through and run the rest of the functions

Here's what I'm seeing:

When answer is not yes or 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

It should have re-asked the questions again.

When answer is indeed yes or 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

^ this is correct.

What am I missing when the condition is not "yes" or "y" that is preventing the function from asking the questions again?

ps this is the function that is asking the questions in case you were curious, or if this helps

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

Thanks!

Andrew Hewitt
  • 518
  • 4
  • 14
chowpay
  • 1,515
  • 6
  • 22
  • 44
  • 2
    `whatever or 'y'` is always true, since the string `'y'` is truthy. The equality test is not distributed by the `or`. You need to use `infoCorrect.lower() == "yes" or infoCorrect.lower() == "y"` or maybe `infoCorrect.lower() in ["yes", "y"]`. – Blckknght Oct 18 '16 at 05:35
  • @Blckkngt I think I get what you're saying but why isn't it distributed? it seems logical does "incorrect" not apply to both conditions? – chowpay Oct 18 '16 at 06:02
  • The `or` doesn't ever see the operator, only the value of the result. So `"foo" == "bar" or "foo"` is equivalent to `False or "foo"`. It then short-circuits and returns `"foo"` (which is truthy). – Blckknght Oct 18 '16 at 06:07

1 Answers1

1

The if condition should be if infoCorrect.lower() == "yes" or infoCorrect.lower() == "y":.

Nurjan
  • 5,889
  • 5
  • 34
  • 54