调用其他脚本并使用变量(Python)

时间:2013-10-24 01:16:49

标签: python scripting module

我不知道该怎么做......

我想使用import转到另一个脚本(一旦调用它,原始脚本已经完成)但我需要第二个脚本来打印原始变量。

所以,我可以导入第二个脚本并使用打印机,但是如果我尝试导入原始脚本以便我可以访问变量..

但如果我这样做,它只会给我一个错误:

Traceback (most recent call last):

File "C:\Users\luke\Desktop\k\startGame.py", line 2, in <module>
    import Storyline
  File "C:\Users\luke\Desktop\k\Storyline.py", line 1, in <module>
    import startGame
  File "C:\Users\luke\Desktop\k\startGame.py", line 56, in <module>
    Storyline.startGame1()


AttributeError: 'module' object has no attribute 'startGame1'

我正在尝试打印这个:

 print ("I see you have picked " + startGame.currentPokemon)

我称之为:

Storyline.startGame1()

,currentPokemon是

currentPokemon = inputKK

(InputKK是初学者口袋妖怪的输入)

有没有办法做到这一点?是的,我正在用Python制作一个口袋妖怪游戏,但它是一个不使用真正的口袋妖怪名字的版本..


故事情节剧本:

import startGame

def startGame1():
    print ("Welcome to the H.Q of I.O.D")
    print ("I am Professor Steel.")
    print ("I see you have picked " + startGame.currentPokemon)

startGame脚本:

import Storyline
inputKK = input("Choose from, 'Craigby', 'Robinby' or 'KKby' ")


    if(inputKK == "Craigby"):
        print("Craigby is a electric type.")
        print("Craigby: Attack = 7, Defence = 3, Health = 6, Speed = 12")
    if(inputKK == "Robinby"):
        print("Robinby is a fire type.")
        print("Robinby: Attack = 6, Defence = 5, Health = 7, Speed = 7")
    if(inputKK == "KKby"):
        print("KKby is a water type.")
        print("KKby: Attack = 5, Defence = 8, Health = 11, Speed = 5")

    print("")

    os.system('cls')

currentPokemon = inputKK
counter = 0;
while(counter < 1):
    print("Welcome to pokeby.")
    print("Type S for [STORYLINE]")
    print("Type R for pokemon in the field [CURRENT IS GRASS] ")
    print("Type Q for [QUIT]")


    inputMainMenu = input("S/R/Q ...")

    if(inputMainMenu == "S"):
        os.system('cls')
        counter = counter + 2
        Storyline.startGame1()
    if(inputMainMenu == "R"):
        os.system('cls')
        counter = counter + 2
    if(inputMainMenu == "Q"):
        os.system('cls')
        inputExit = input("Are you sure you want to quit? Y/N ")
        if(inputExit == "Y" or inputExit == "y"):
            print("K")
        else:
            counter = counter + 1

3 个答案:

答案 0 :(得分:0)

您正试图import Storyline进入startGame,并尝试import startGame进入Storyline。你不能做这种递归导入。 import startGameStoryline Storyline.startGame1() startGame1()调用{{1}}之前{{1}}已定义,因此您会收到无属性错误。

您应该重新构建文件,以免变得不必要。

答案 1 :(得分:0)

import StartGame脚本中不要Storyline。相反,只需将所需的值传递给StartGame1函数。

# Storyline.py
def startGame1(currentPokemon):
    print ("Welcome to the H.Q of I.O.D")
    print ("I am Professor Steel.")
    print ("I see you have picked ", currentPokemon)

然后在startGame中,您呼叫Storyline.startGame1(inputKK)传递宠物小精灵的名字。

顺便说一句,你的startGame1函数不在模块startGame中有点令人困惑......

答案 2 :(得分:-1)

[编辑:不要听我的话;已经很晚了,我对自己所说的内容并不够认真。]

您无法在模块中引用属性或方法。 你需要的是把你的方法放在一个类中。或者,我认为你可以做from Storyline import startGame1()。但实际上,使用类[如果你愿意]; [我认为]他们很好。 Python docs on classes here.