2016-08-14 126 views
0

我正在(嘗試)製作一個像Hack Run或Hacknet這樣的黑客遊戲。但只有終端。我得到這個錯誤,當我嘗試在線路86來打印變量「currentip」(「打印(」您目前在「+ currentip +」「)。」):我在我的Python代碼中找不到錯誤

UnboundLocalError: local variable 'currentip' referenced before assignment 

這看起來像一個簡單的錯誤但我無法弄清楚。我已經分配了它。多次。也許我正在閱讀訂單執行錯誤,但我找不到任何信息,說我做錯了...

任何想法清理,使它更整潔/更好也非常讚賞。

import os 
import random 
from time import sleep 
os.system("cls") 

save = {} 
ips = {"1337.1337.1337.1337": "Cheater's Stash"} 
shells = [] 
storyips = ["Bitwise Test PC"] 
currentip = "1.1.1.1" 
homeip = "1.1.1.1" 

def resetip(): 
    ip1 = random.randint(1, 999) 
    ip2 = random.randint(1, 999) 
    ip3 = random.randint(1, 999) 
    ip4 = random.randint(1, 999) 
    homeip = str(ip1) + "." + str(ip2) + "." + str(ip3) + "." + str(ip4) 
    if homeip in ips: 
    resetip() 
    else: 
    ips[homeip] = "Your Computer" 
    currentip = homeip 

def storyreset(): 
    for x in storyips: 
    ip = (0, 0, 0, 0) 
    ip1 = random.randint(1, 999) 
    ip2 = random.randint(1, 999) 
    ip3 = random.randint(1, 999) 
    ip4 = random.randint(1, 999) 
    ip = str(ip1) + "." + str(ip2) + "." + str(ip3) + "." + str(ip4) 
    if ip in ips: 
     storyreset() 
    else: 
     ips[ip] = x 

def start(): 
    os.system("cls") 
    print("Python 3.5, HackSim 1.1") 
    print("") 
    print("Loading modules...") 
    print("") 
    sleep(1) 
    print("OS Loaded.") 
    sleep(0.5) 
    print("HELP Loaded.") 
    sleep(0.5) 
    print("FILE USE Loaded.") 
    sleep(1) 
    print("CONNECTIONS Loaded.") 
    sleep(0.5) 
    print("UTILS Loaded.") 
    sleep(0.5) 
    print("HACKS Loaded.") 
    print("") 
    sleep(1) 
    print("Initiating command line...") 
    sleep(1) 
    commandline() 

def usecommand(c): 
    if c == "reboot": 
    print("Rebooting...") 
    sleep(3) 
    start() 
    elif c == "clear": 
    os.system("cls") 
    elif c == "quit": 
    quit() 
    elif c == "forkbomb": 
    del ips[currentip] 
    if homeip in ips: 
     currentip = "Your Computer" 
    else: 
     resetip() 
     currentip = "Your Computer" 
    elif "connect " in c: 
    if c[8:] in ips: 
     connectip = ips[c[8:]] 
     print("Connecting to ", connectip, " ", c[8:], "...") 
     currentip = connectip 
    else: 
     print("This ip does not exist.") 
    elif c == "connect": 
    print("You are currently at " + currentip + ".") 
    print("The syntax of this command is: connect <ip>.") 
    else: 
    print("Invalid command. Either the command does not exist or check the required syntax.") 

def commandline(): 
    while True: 
    command = input("> ") 
    usecommand(command) 

storyreset() 
resetip() 
start() 

謝謝!

+0

請在這裏發佈源代碼的相關部分以及運行該程序時得到的錯誤堆棧跟蹤。 –

+0

Stacktrace:http://pastebin.com/DkYdgPDV –

+0

至於相關部分...我不知道。我是一名Python初學者。 –

回答

2

問題是你的代碼中有全局變量,並且你試圖從函數內部訪問它們,而沒有首先聲明它們是全局變量。您需要在usecommand函數的開頭放置一行global currentip

另外請注意,如果你只在函數中使用變量currentip這是可行的,但如果你使用它,同樣的功能內分配它既解釋假定它是您使用的是局部變量。看看這個:

x = 10 

def f(): 
    print x 

def f2(arg): 
    if arg: 
     x = 20 
    else: 
     print x 

運行功能f()將打印10,但運行功能f2(0)因爲解釋再次不確定您正在使用的變量是否是局部或全局的,並假定它會產生一個錯誤一個本地的。

HTH。

+0

更具體地說,OP正在修改該值,純讀取是好的,也就是說,「訪問」有點兒誤導。 – YiFei

+0

@YiFei,我修改了我的答案,包括一些關於這個問題的更多信息:) – Victor

+0

Ohhhhh ....我明白了。我不知道全球和本地變量如此重要。我以前從未有過這個錯誤。我認爲,golbal變量可以用於不同的程序等。不要問我你會怎麼做,或者爲什麼我認爲這樣做。感謝維克多的快速回應。 –

相關問題