2016-08-14 3 views
0

해킹이나 해킹과 같은 해킹 게임을 만들려고합니다. 그러나 터미널 만. 내가 라인 86에서 변수 'currentip을'인쇄하려고 할 때이 오류가 ("인쇄 ("당신은에서 현재 "+ currentip +" ")가".) :파이썬 코드에서 오류를 찾을 수 없습니다.

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

관련 부분은 ... 단서가 없습니다. 저는 파이썬으로 초보자입니다. –

답변

2

문제는 코드에 전역 변수가 있고 전역 변수를 먼저 선언하지 않고 함수 내부에서 액세스하려고하는 것입니다. usecommand 함수의 시작 부분에 라인 global currentip을 입력해야합니다.

또한 만 함수에서 변수 currentip를 사용하는 경우 작동 것이 있습니다,하지만 당신은 모두를 사용하고 동일한 기능 내에서 할당하는 경우 인터프리터는 당신이 사용하는 로컬 변수로 가정합니다. 이것 좀보세요 : 인터프리터가 다시 한 번 사용하는 변수가 로컬 또는 글로벌 여부를 확신하고 그것이 가정하기 때문에 오류가 발생합니다 기능 f2(0)10을 인쇄 할 기능 f()을 실행하지만, 실행

x = 10 

def f(): 
    print x 

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

지방의 것.

HTH.

+0

보다 구체적으로, OP는 그 값을 수정하고 있습니다. 순수한 읽기는 괜찮습니다. 즉, "액세스"가 오해의 소지가 있습니다. – YiFei

+0

@YiFei, 나는 주제에 대한 좀 더 많은 정보를 포함하기 위해 나의 대답을 수정했다 :) – Victor

+0

Ohhhhh .... 알 겠어. 나는 글로벌 변수와 로컬 변수가 그렇게 중요하다는 것을 몰랐다. 전에이 오류가 없었어요. 나는 골볼 변수가 다른 프로그램 등에서 사용될 수 있다고 생각했습니다. 당신이 어떻게 할 것인지, 왜 그렇게 생각하는지 묻지 마십시오. 신속한 대응을 위해 Victor에게 감사드립니다. –

관련 문제