Why is my function looping python
<p>Weird issue I know that a function is being called twice some how but I dont know where or why its happening.</p>

<p>Heres my code:</p>

<pre><code>  8 def getAccessSecretName():
  9         access_key = raw_input("Enter Access Key: ")
 10         secret_key = raw_input("Enter Secret Key: ")
 11         yourName = raw_input("Enter your name: ")
 12         print "Access Key: %s" % access_key
 13         print "Secret Key: %s" % secret_key
 14         print "Your full name is: %s" % yourName
 15         with open (tfVariables,"w") as text_file:
 16                 text_file.writelines(['access_key = \"'+ access_key +'\"\nsecret_key = \"'+ secre    t_key +'\"\n\n\n',
 17                                 'amis = {\n',
 18                                 '   ',
 19                                 'us-west-1 = '+ usWest1ami +'\n',
 20                                 '   ',
 21                                 'us-west-1 = '+ usWest2ami +'\n',
 22                                 '   ',
 23                                 '}'])
 24         return access_key, secret_key, yourName

 69 def makeMainTF():
 70         NameTag,mcGroupTag,mcIPTag = makeNameMCTag()
 71         access_key, secret_key, yourName = getAccessSecretName()
 72         with open (tfFileName,"w") as text_file:
 73                 text_file.writelines(['provider \"aws\" {\n',
 74                                       '  ',
 75                                         'access_key = \"${var.access_key}\"\n    ',
 76                                       '  ',
 77                                         'secret_key = \"${var.secret_key}\"\n    ',
 78                                       '  ',
 79                                         'region     = \"${var.access_key}\"\n    ',
 80                                       '}\n\n\n',
 81                                       'resource \"aws_instance\" \"example\"     {\n',
 82                                         '  ',
 83                                       'ami = \"${lookup(var.amis, var.region)    }\"\n',
 84                                         '  ',
 85                                         'instance_type = \"%s\" \n}' % instan    ceType,
 86                                         '\n\n\n\n',
 87                                         'tags {\n',
 88                                         '   ',
 89                                         'Name = \"%s\"\n' % NameTag,
 90                                         '   ',
 91                                         'Multicast = \"%s,%s\"\n' % (mcGroupT    ag,mcIPTag),
 92                                         '   ',
 93                                         #'Owner = \"%s\"' % " " % yourName,
 94                                         'Owner = \"%s\"' % yourName,
 95                                         '\n}\n\n\n'])
</code></pre>

<p>So here is what I expect to happen. When I run the code it will prompt the user for its access key, secret key and name. Then repeat it to the user once and write the information to a file. What happens when I call on both functions is this:</p>

<pre><code>Enter Access Key: key1
Enter Secret Key: secret1
Enter your name: chowpay
Access Key: key1
Secret Key: secret1
Your full name is: chowpay
newnumber = 68
Name Tag: vlslabs67
Multicast Tag: vlslabmc, 172.16.0.67
Enter Access Key: key2
Enter Secret Key: secret2
Enter your name: chowpay2
Access Key: key2
Secret Key: secret2
Your full name is: chowpay2
</code></pre>

<p>Note that it's prompting the user to enter The keys and their name twice. Which doesnt make sense as this is all I have used to call the function:</p>

<pre><code>getAccessSecretName()
makeMainTF()
</code></pre>

<p>Thanks!</p>

<hr>

<p>I've corrected the code above with the correct function in line 53, it is getAccessSecret<strong>Name</strong>() , not getAccessSecret()</p>

<p>Correction again for adding the wrong function makeTfVars was posted in the question but makeMainTF() was , what was suppose to be in the question.</p>

<p>What I thought was a problem in the loop was a issue with how I thought variables in python get passed to other functions.</p>

<p>looks like doing this </p>

<pre><code>71: access_key, secret_key, yourName = getAccessSecretName()
</code></pre>

<p>Doesnt mean make ^ variable values available from the function of getAccessSecretName(). Dumb mistake by me. Instead it means re-run the function to get the values which caused the user to be asked questions again.</p>

<p>The "fix" is adding</p>

<pre><code> 10 def getAccessSecretName():
 **11         global access_key, secret_key, yourName**
</code></pre>

<p>and removing </p>

<pre><code>  71: access_key, secret_key, yourName = getAccessSecretName()
</code></pre>

<p>This let me use my variables in other functions. Not sure if this is the right way to do it but it worked for me. Welcome to hear what a better way would be.</p>

<p>Thanks again</p>

python loops