我不知道该怎么做......
我想使用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
答案 0 :(得分:0)
您正试图import Storyline
进入startGame
,并尝试import startGame
进入Storyline
。你不能做这种递归导入。 import
startGame
时Storyline
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)
[编辑:不要听我的话;已经很晚了,我对自己所说的内容并不够认真。]
您无法在模块中引用属性或方法。 你需要的是把你的方法放在一个类中。或者,我认为你可以做使用类[如果你愿意]; [我认为]他们很好。 Python docs on classes here. from Storyline import startGame1()
。但实际上,