我已经检查了有关此错误的其他问题,但它们看起来与我尝试做的有点不同,没有直接的用户输入,所以希望您可以提供帮助。
使用网格中的值我正在尝试编写能够解决网格上两个位置之间差异的代码。 Playerpos是玩家的当前位置,bossgroup代表网格(或地图)上的固定点,取自平行的老板地图。
# playerposition, as row,column...
playerpos = [0,0]
a = playerpos[0]
b = playerpos[1]
# here I make it so the player can set a location in the bossmap
# as a fixed location called bossgroup. Build a HQ in game terms...
bossmap[playerpos[1]][playerpos[0]] = bossgroup
OR - 如果count低于计数值,则使用上述语句的计数值,但如果超过计数值,则使用以下语句创建bossgroup
。到目前为止,无效的文字错误总是发生在计数之下,但我在这里包括两个语句,以防它相关。
# seems necessary to fomat bossgroup, to look like "1, 7" rather than "17"
bossgroup = str(a)+str(", ")+str(b)
# here coordinates are striped from the string describing bossgroup
# and made into integers so math can be done....
c = int(bossgroup[0])
d = int(bossgroup[3])
# now i want to turn the differences between the two sets of row and columns in x and y values and do math to them, including using abs() to flip negs to positives
x = a - c
y = b - c
...
然而我遇到的问题是我一直收到这个错误 -
d = int(bossgroup[3])
ValueError: invalid literal for int() with base 10: ''
奇怪的是,这并不总是发生,这取决于我所做的数学,但是现在我觉得我已经在屏幕上放了足够的代码供你帮助,希望它足够清楚和充分看看我做错了什么。
答案 0 :(得分:2)
您的错误很明确,来自以下几行:
bossgroup = str(a)+str(", ")+str(b)
c = int(bossgroup[0])
d = int(bossgroup[3])
当a大于10时发生。 例: 对于a = 10和b = 1 ,您将获得 bossgroup =“10,1”, c = int('1')(其中是bossgroup中的第一个字符)和 d = int('')(这是bossgroup中的第四个字符),这是你的错误,你不能将字符串转换为int。
计算两个点a和b之间的距离(差异)的更好方法是:
import math
d = math.sqrt( (b[0]-a[0])**2 + (b[1]-a[1])**2 )