2016-10-11 66 views
-2

奇怪的问题我知道一个函数被调用了两次,但我不知道它发生的地方或原因。为什么我的函数循环python

继承人我的代码:

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

所以这是我期待的事情发生。当我运行代码时,它会提示用户访问密钥,密钥和名称。然后重复给用户一次并将信息写入文件。当我呼叫这两种功能时会发生什么:

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 

请注意,它提示用户输入两次密钥及其名称。这没有道理,因为这是我已经习惯了调用该函数:

getAccessSecretName() 
makeMainTF() 

谢谢!


我在第53行正确的函数修正上面的代码,它是getAccessSecret 名称(),而不是getAccessSecret()

修正再次添加了错误的功能makeTfVars被张贴在问题,但makeMainTF()是,这个问题应该是什么。

+3

要调用'getAccessSecretName()'从调用代码,并再次从'makeTfvars()',但你不显示如自调用。 – cdarke

+3

谁是'getAccessSecret()'? – essramos

+0

也请不要复制行号。 – Julien

回答

0

我认为是循环中的一个问题是我如何认为python中的变量传递给其他函数的问题。

看起来像这样

71: access_key, secret_key, yourName = getAccessSecretName() 

并不意味着让^从getAccessSecretName的函数变量值()。愚蠢的错误由我。相反,它意味着重新运行该函数以获取导致用户被再次提问的值。

“修复” 被添加

10 def getAccessSecretName(): 
**11   global access_key, secret_key, yourName** 

和删除

71: access_key, secret_key, yourName = getAccessSecretName() 

这让我用我的变量等功能。不知道这是否是正确的做法,但它对我有用。欢迎听听会有更好的方式。

再次感谢