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

2024-07-02 11:01:05 发布

您现在位置:Python中文网/ 问答频道 /正文

我不知道该怎么办。。。你知道吗

我想使用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是启动程序pokemon的输入)

有什么办法吗?是的,我正在用Python制作一个pokemon游戏,但是这个版本没有使用真正的pokemon名称。。你知道吗


故事情节脚本:

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

Tags: inimport脚本youifcountermoduleprint
3条回答

不要在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中,这有点令人困惑。。。你知道吗

你试图import Storyline进入startGame,同时也试图import startGame进入Storyline。你不能做这种递归导入。当import调用startGame时,Storyline在定义startGame1()之前遇到了您的Storyline.startGame1()调用,因此您得到了no属性错误。你知道吗

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

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

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

相关问题 更多 >