2016-08-14 127 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变量可以用于不同的程序等。不要问我你会怎么做,或者为什么我认为这样做。感谢维克多的快速回应。 –

相关问题