我剛剛用圖靈機器親自化了。我正在製作一個半精確的虛擬圖靈機,但我遇到了一個看似簡單的問題,我只知道有一個解決方案。我在網上進行了研究,但找不到滿足我的問題的任何事情。如何在函數前定義一個全局變量,然後在python中的函數內引用它?
如何在函數中調用變量'l'。它必須是可調用的,因爲如果我定義函數中的變量,當函數循環的初始值將重置價值爲0。
這裏是我的代碼:
blanktape = []
for x in range(1,251):
x = ' '
blanktape.append(x)
global l
l = 1
non = ' '
head = blanktape[l]
symbols = [3, 'ee', 'x']
def mconfigb():
if head == non:
blanktape[blanktape.index(head)] = 0
l = l + 2
def mconfigc():
if head == non:
blanktape[blanktape.index(head)] = 1
l = l + 2
def turingmachine():
while l < len(blanktape) + 1:
mconfigb()
mconfigc()
return blanktape
print turingmachine()
我會鸚鵡評論在下面的答案中提出,但我同意他們。首先,它是一個使用課堂的絕佳機會。其次,一個字母變量往往是很難處理的。不要害怕像head_location或類似的東西使用更長的名字。 – 2013-03-20 00:20:10