-2

I'm trying to get the variable 'money' from the file 'gold.py' to the file 'sandy1.py', but it seems impossible to get it over. I've tried from gold.py import money but that just imports the whole thing and runs it.

Code from 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'

Code from 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')

Sorry if it's messy, it's the best way I could transfer it.

Padraic Cunningham
  • 176,452
  • 29
  • 245
  • 321
Alex Carr
  • 11
  • 4

3 Answers3

0

You need to check for the main running file:

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

Basically, these statements allow them to run on their own or with one another. The main file imports money without changing it's value to 100. This is because it is not the main file due to the nature of the import. However, in sandy1.py the money will be printed because it is the main file.

Malik Brahimi
  • 16,341
  • 7
  • 39
  • 70
0

The name of an imported module does not include the .py extension. You should import from gold, not from gold.py:

from gold import money
import os
health = '100'
# etc. etc.
brandizzi
  • 26,083
  • 8
  • 103
  • 158
  • 1
    Your code has many other problems, but I believe you are going to explore it a bit more so I'll not annoy you trying to solve them before you discover them :) – brandizzi Jan 21 '15 at 21:15
-1

Like said above you can use if __name__ == "__main__": this goes at the bottom of your file and with this you can call what ever functions you want to be run. So just make your money variable a function. Another solution would to just call you function using bash from python.

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

don't forget to change your directory to the path that you saved your gold.py. Although this will not be a variable so the best solution is this.

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

This will assign the output of your bash command to the variable output. I did print output[:-1] because usually the variable will have "\n" at the end for some reason so I just excluded that.