gpt4 book ai didi

来自变量内容的 Python NameError

转载 作者:行者123 更新时间:2023-11-30 23:36:44 24 4
gpt4 key购买 nike

我一直在为 Minecraft 的 Raspberry Pi 版本制作 mod,每次在程序中输入其中一个命令时,我都会遇到非常令人沮丧的错误。这是我的代码:

import minecraft.minecraft as minecraft
import minecraft.block as block
import time

mc = minecraft.Minecraft.create();

print('newBlock - Change ID of block to spawn')
print('blockType - Change subID of block to spawn')
print('pos1')
print('pos2')
print('fill - fill specified area')
print('clear - clear specified area')
print
while True:
comm=str(input('Command: '))
if comm=="newBlock":
blockId = int(input('Enter Block ID: '))
mc.postToChat('Block set to ID: ' + str(blockId))
if comm=="blockType":
blockData = int(input('Enter Block Type: '))
if comm=="pos1":
position1 = mc.player.getPos()
mc.postToChat('Set Position 1 as: x' + str(position1.x) + ' y' + str(position1.y) + ' z' + str(position1.z))
if comm=="pos2":
position2 = mc.player.getPos()
mc.postToChat('Set Position 2 as: x' + str(position2.x) + ' y' + str(position2.y) + ' z' + str(position2.z))
if comm=="fill":
mc.setBlocks(position1.x, position1.y, position1.z, position2.x, position2.y, position2.z, blockId, blockType)
mc.postToChat('Filled specified area with: ' + str(blockId))
if comm=="clear":
mc.setBlocks(position1.x, position1.y, position1.z, position2.x, position2.y, position2.z, 0)
mc.postToChat('Cleared specified area')

每次用户通过变量“comm”输入输入时,程序都会发出此错误消息:

Traceback (most recent call last):
File "WorldEditPi.py", line 15, in <module>
comm=str(input('Command: '))
File "<string>", line 1, in <module>
NameError: name 'newBlock(or what ever the user entered into 'comm')' is not defined

真正令人困惑的是,它甚至没有谈论变量“newBlock”不是变量,它是变量“comm”的内容。所有命令都会出现这种情况,而不仅仅是“newBlock”。

最佳答案

您在需要使用 raw_input 的地方使用了 inputinput 计算它传递的字符串。 raw_input 为您提供一个字符串,这就是您想要的。

请注意,这仅适用于 Python 2。在 Python 3 中,raw_input 不再可用,input 相当于 Python 2 的 raw_input >。在 Python 2 中,input 相当于 eval(raw_input)

关于来自变量内容的 Python NameError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16258974/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com