如何在Python中将变量从一个文件传输到另一个文件

时间:2015-01-21 20:49:54

标签: python variables transfer

我试图获取变量' money'来自文件' gold.py'到文件' sandy1.py',但似乎无法完成它。我已经尝试了from gold.py import money,但只是导入整个事情并运行它。

Sandy1.py的代码:

from gold.py import money
import os
health = '100'
print 'Welcome to Sandy, the modern day text-based RPG! Type help to view commands.'
while 1 + 1 == 2:
    main = raw_input('')    
    if main == 'suicide':
        print 'Goodbye cruel world...'
        health = '0'
        print 'You died, type respawn to reset your health.'
        main

    if main == 'respawn':
        print '*Your soul re-enters your body and you come back to life*'
        health = '100'
        main

    if main == 'health':
        print 'Your health is %r' % (health) 
        main = raw_input('')

    if main == 'quests':
        print 'Quest List: Gold, The Creep'
        main

    if main == 'Gold':
        os.system('gold.bat')
        main

    if main == 'money':
        print 'You have %d coins' % (money)
        main

    if main == 'help':
        print 'Commands: suicide, respawn, health, quests, money'

来自gold.py的代码:

import os

money = '0'
health = '100'

 print 'You are at home waiting for the mail, when the mailman comes rushing up to you'
 print 'Mailman: Oi, you, on the way up a bear attacked me and I lost all my gold, could you go get it?'
type = raw_input('')
if type == 'ok':
    print 'Mailman: Thanks so much!'
    print 'Quest started: Gold'
    print 'You begin to venture out of your garden and down the lane, when you encounter the bear. Do you fight it, hide from it, or run back home?'
    part1 = raw_input('')
    if part1 == 'fight it':
        print 'You ready your fist to fight the bear, when it leaps on you and rips your head off in one. GAME OVER!'
        health = '0'
        os.system('sandy1.bat')
    if part1 == 'run back home':
        print 'You escaped with your life... But you failed the quest. GAME OVER!'
        os.system('sandy1.bat')
    if part1 == 'hide from it':
        print 'You run and hide from it. It only just misses you. Whew, that was a close one.'
        print 'As you carry on walking, you see a stream. Do you jump it, swim it, or go around it?'
        part2 = raw_input('')
    if part2 == 'swim it':
        print 'You try to swim across but get eaten by a crocodile. GAME OVER!'
        health = '0'
        os.system('sandy1.bat')
    if part2 == 'go around it':
        print 'You start to run around it, but you run into another bear on the way. GAME OVER!'
        health = '0'
        os.system('sandy1.bat')
    if part2 == 'jump it':
        print 'You successfully jump over the stream and land safely on the other side.'
        print 'There it is, the gold! You run over and grab the sack, then go back home, where you return it to the mailman.'
        print 'You: Here you go, sir.'
        print 'Mailman: Thank you. Here is some money for your deeds.'
        print 'You gain 5 coins'
        money = '5'
        print 'Quest Completed: Gold'
        os.system('sandy1.bat')

对不起,如果它很乱,这是我转移它的最佳方式。

3 个答案:

答案 0 :(得分:0)

您需要检查主要运行文件:

gold.py

money = 1000000

if __name__ == '__main__':
    money = 100 # never actually called

sandy1.py

from gold import money 

if __name__ == '__main__':
    print money # outputs 1000000

基本上,这些语句允许它们独立运行或彼此运行。主文件导入money而不将其值更改为100.这是因为由于导入的性质,它不是主文件。但是,在sandy1.py中,钱会被打印,因为它是主文件。

答案 1 :(得分:-1)

导入模块的名称不包含.py扩展名。您应该从gold导入,而不是从gold.py导入:

from gold import money
import os
health = '100'
# etc. etc.

答案 2 :(得分:-1)

如上所述,您可以使用 if __name__ == "__main__":这位于文件的底部,您可以通过它调用您想要运行的功能。所以只需让你的钱变量成为一个功能。 另一种解决方案是使用python中的bash调用函数。

import os, sys
os.system(python gold.py)

不要忘记将目录更改为保存gold.py的路径。虽然这不是一个变量,所以最好的解决方案就是这个。

import subprocess output = subprocess.check_output("python (directorypath)/gold.py", shell=True) print output[:-1]

这会将bash命令的输出分配给变量输出。我打印输出[: - 1]因为通常变量会有" \ n"最后由于某种原因所以我把它排除在外。

相关问题