2016-08-14 3 views
0

にエラーを見つけることができません。しかし、端末だけ。私はライン86で変数「currentip」を印刷しようとすると、私はこのエラーを取得します(「印刷(」現在の位置は「+ currentip +」「)は、」。):私はハックを実行したりHacknetのようなハッキングゲームを作るために(しよう)しています私の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

スタックトレース:http://pastebin.com/DkYdgPDV –

+0

関連する部分については...私は見当もつかない。私はPythonの初心者です。 –

答えて

2

問題は、あなたのコード内でグローバル変数を持っていて、最初にそれらがグローバル宣言せずに関数内からそれらにアクセスしようとしているということです。 usecommand関数の先頭に、global currentipという行を置く必要があります。

はまた、あなたが唯一のあなたの関数内の変数currentipを使用した場合、それがうまくいくことに注意していますが、両方を使用して、同じ関数内でそれを割り当てている場合はインタプリタは、それはあなたが使用しているローカル変数であると想定します。これを見てください:機能f()を実行

x = 10 

def f(): 
    print x 

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

10を印刷しますが、通訳が再び使用している変数がローカルかグローバルかが不明であり、それはあると仮定しているため機能f2(0)を実行すると、エラーが生成されます地元のもの。

HTH。

+0

より具体的には、OPはその値を変更しています。純粋な読み取りは問題ありません。つまり、「アクセス」は誤解を招くことがあります。 – YiFei

+0

@YiFeiは、私が主題:) – Victor

+0

Ohhhhhが....私が見にいくつかのより多くの情報を含めるように私の答えを変更しました。グローバル変数とローカル変数がそれほど重要であることはわかりませんでした。私は前にこのエラーがあったことはありません。私はゴルバン変数がさまざまなプログラムなどで使用できると思いました。あなたがそれをどうやって行うのか、それをどう思ったのか、私に尋ねないでください。あなたの迅速な対応のためVictorに感謝します。 –

関連する問題