我正在爲學習目的製作基於文本的平臺遊戲。我希望所有的移動功能都在一個單獨的python文件中,但我遇到了讓他們一起工作的問題。Python:如何通過循環導入調用另一個文件中的函數
我有主要的遊戲叫做floors.py
,地圖文件是floormap.py
。
我可以導入並運行floormap.py
中的功能floors.py
內部完全沒關係。
但是我不知道如何在運行floormap.py
函數後返回floors.py
函數。下面是一個例子。當我運行它,我得到了終端出現以下錯誤:
NameError: global name 'first_hall_1' is not defined
我沒有得到這個利用工作:
from floormap import first_hall_1
但我能找到一種方式來獲得再次被調用的函數原始文件。
Floors.py:
import floormap
def first_hall_object():
grab = raw_input("Enter Command > ")
backward = ['back', 'Back', 'Backward', 'backward']
if any (s in grab for s in backward):
first_hall_1()
def walkin_hall():
print "whatever"
floormap.py:
import floors
def first_hall_1():
print "You are in front of the door again. It is locked."
walkin_hall()
嘗試重新組織你的代碼,以便這兩個模塊不互相依賴這樣。例如,使floor.py導入floormap並調用floormap函數,但_do't沒有floormap.py也導入樓層和調用樓層函數。循環進口是合法的,但它們可能導致一個痛苦的世界。 –