1

我不知道该怎么做...

我想使用 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 = 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)

开始游戏脚本:

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
4

3 回答 3

0

您正在尝试import Storyline进入startGame,也正在尝试import startGame进入Storyline。你不能做这种递归导入。虽然importing startGame,在定义之前Storyline遇到你的Storyline.startGame1()调用startGame1(),所以你得到 no attribute 错误。

你应该重组你的文件,这样就没有必要了。

于 2013-10-24T01:30:20.750 回答
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......

于 2013-10-24T01:34:06.013 回答
-1

[编辑:别听我的;已经很晚了,我没有认真考虑我在说什么。]

您不能在这样的模块中引用属性或方法。你需要的是把你的方法放在一个类中。或者,我认为你可以这样做from Storyline import startGame1()。但实际上,使用类 [如果你愿意];[我认为]他们很好。关于类的 Python 文档在这里。

于 2013-10-24T01:20:44.620 回答