2013-10-24 18 views
1

我不知道如何去這個...調用其它的腳本和使用變量(Python)的

我想用進口到另一個腳本(一旦它被稱爲,最初的劇本已經完成)但我需要第二個腳本來從原始打印一個變量。

所以,我可以導入第二個腳本,並使用打印正常,但是如果我嘗試並導入原來的劇本,所以我可以訪問變量..

但如果我這樣做,它只是給我一個錯誤:

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

你將來需要一個更具描述性標題。也許是下一次的實際問題? – dckuehn

+0

請將'startGame.py'的內容和'Storyline.py'中的函數列表發佈 – inspectorG4dget

+0

將其更改爲更合適的內容,雖然不能真正描述它.. – user2913720

回答

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在...

+0

是的,startGame1只是一個佔位符,但我重新構建了腳本。並感謝您的答案! – user2913720

-1

[編輯:不聽我說;它已經晚了,我沒有足夠認真地對待我說的話。]

你不能在這樣的模塊中引用屬性或方法。 你需要的是把你的方法放在課堂上。另外,我認爲你可以做from Storyline import startGame1()。但真的,使用類[如果你想]; [我認爲]他們很好。 Python docs on classes here.

+0

錯誤。 '進口故事情節; Storyline.myFunction()'與Storyline import myFunction中的'相同; myFunction的()'。類是有用的,但不是絕對必要的 – inspectorG4dget

+0

此外,當功能更好地完成工作時,不要使用類。 –

+0

感謝您的更正。雖然這裏的課程不會很好嗎? startGame()可能是一個很好的功能,但我可以想象每個Storyline會更好地被當作一個類來對待。我想這一切都取決於底層代碼。我想我可能過度使用課堂。 – 7yl4r

0

您正在嘗試import StorylinestartGame,並且還試圖import startGameStoryline。你不能做這種遞歸導入。雖然import ing startGame,Storyline正在通過您的Storyline.startGame1()呼叫,但在startGame1()已被定義之前,因此您會收到no屬性錯誤。

你應該重構你的文件,這樣就不必要了。

相關問題