我在Python代码中找不到错误

我在Python代码中找不到错误,python,Python,我正在(尝试)制作一款黑客游戏,比如Hack Run或Hack Net。但只有终点站。当我试图在第86行打印变量“currentip”(打印(“您当前在“+currentip+”))时,会出现此错误: 这看起来像一个简单的错误,但我无法找出它。我已经分配了。多次。也许我读错了订单执行,但我找不到任何信息表明我做错了 我们也非常感谢任何关于清洁和使其更整洁/更好的想法 import os import random from time import sleep os.system("cls")

我正在(尝试)制作一款黑客游戏,比如Hack Run或Hack Net。但只有终点站。当我试图在第86行打印变量“currentip”(打印(“您当前在“+currentip+”))时,会出现此错误:

这看起来像一个简单的错误,但我无法找出它。我已经分配了。多次。也许我读错了订单执行,但我找不到任何信息表明我做错了

我们也非常感谢任何关于清洁和使其更整洁/更好的想法

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()
导入操作系统
随机输入
从时间上导入睡眠
操作系统(“cls”)
保存={}
ips={“1337.1337.1337.1337”:“骗子的藏匿处”}
贝壳=[]
storyips=[“按位测试PC”]
currentip=“1.1.1.1”
homeip=“1.1.1.1”
def resetip():
ip1=random.randint(1999)
ip2=random.randint(1999)
ip3=random.randint(1999)
ip4=random.randint(1999)
homeip=str(ip1)+“+str(ip2)+”+str(ip3)+“+str(ip4)
如果在ips中使用homeip:
resetip()
其他:
ips[homeip]=“您的计算机”
当前IP=家庭IP
def storyreset():
对于storyips中的x:
ip=(0,0,0,0)
ip1=random.randint(1999)
ip2=random.randint(1999)
ip3=random.randint(1999)
ip4=random.randint(1999)
ip=str(ip1)+“+str(ip2)+”+str(ip3)+“+str(ip4)
如果ip在ip中:
故事重置()
其他:
ips[ip]=x
def start():
操作系统(“cls”)
打印(“Python 3.5,HackSim 1.1”)
打印(“”)
打印(“加载模块…”)
打印(“”)
睡眠(1)
打印(“已加载操作系统”)
睡眠(0.5)
打印(“已加载帮助”)
睡眠(0.5)
打印(“文件使用已加载”)
睡眠(1)
打印(“已加载连接”)
睡眠(0.5)
打印(“加载了UTILS”)
睡眠(0.5)
打印(“已加载黑客”)
打印(“”)
睡眠(1)
打印(“启动命令行…”)
睡眠(1)
命令行()
def USECOMAND(c):
如果c==“重新启动”:
打印(“重新启动…”)
睡眠(3)
开始()
elif c==“清除”:
操作系统(“cls”)
elif c==“退出”:
退出
elif c==“forkbomb”:
删除IP[当前IP]
如果在ips中使用homeip:
currentip=“您的计算机”
其他:
resetip()
currentip=“您的计算机”
c中的elif“连接”:
如果ips中的c[8:]:
connectip=ips[c[8:]
打印(“连接到”,连接,”,c[8:],“…”)
当前IP=连接IP
其他:
打印(“此ip不存在。”)
elif c==“连接”:
打印(“您当前处于“+currentip+”)
print(“此命令的语法为:connect”)
其他:
打印(“无效命令。该命令不存在或检查所需语法。”)
def命令行():
尽管如此:
命令=输入(“>”)
使用命令(命令)
故事重置()
resetip()
开始()

谢谢

问题在于,代码中有全局变量,您试图从函数内部访问它们,而不首先声明它们为全局变量。您需要在
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.

问题在于,代码中有全局变量,您试图从函数内部访问它们,而不首先声明它们为全局变量。您需要在
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.

请在此发布源代码的相关部分以及运行程序时出现的错误stacktrace。stacktrace:至于相关部分。。。我不知道。我是python的初学者。您分配给
currentip
,因此它被认为是本地的。在函数中使用
global currentip
,将其标记为全局。谢谢。但我只想说,我是一个初学者,不知道局部变量不能在函数中使用,我也无法实现搜索“Python变量范围错误”。如果我不知道这个问题,我必须事先知道要搜索的单词。请在这里发布源代码的相关部分以及运行程序时出现的错误stacktrace。stacktrace:至于相关部分。。。我不知道。我是python的初学者。您分配给
currentip
,因此它被认为是本地的。在函数中使用
global currentip
,将其标记为全局。谢谢。但我只想说,我是一个初学者,不知道局部变量不能在函数中使用,我也无法实现搜索“Python变量范围错误”。我必须知道如何具体地搜索它,如果不知道这个问题,我必须事先知道要搜索的词。更具体地说,OP是修改那个值,纯阅读是可以的,也就是说,“访问”有点误导。@YiFei,我已经修改了我的答案,将som包括在内