【问题标题】:Unsupported operand type(s) for /: 'unicode' and 'int'/ 不支持的操作数类型:“unicode”和“int”
【发布时间】:2013-10-25 04:10:35
【问题描述】:

首先,很抱歉在短时间内提出了两个问题,但我解决了最后一个问题,所以我再次需要帮助。我正在使用 jython/python 编写 bukkit 插件...我对 python/jython 很陌生,我不明白我在哪里犯错,看看代码:

(everything is under class hween(PythonPlugin))                      
def CandyChance(self):
    chance = self.cfg.getString("main.candydropchance") #this works, I tried to print it and result is 10 (which I entered in config before)
    chancetotal = chance / 100

@hook.event("block.BlockBreakEvent", "HIGHEST")        
def onBlockBreakEvent(event):
    #something
    chancetotal = pyplugin.CandyChance() 
    if("Random.nextDouble() <= %s"%chancetotal):
       #do something

谢谢!

【问题讨论】:

  • 什么是完整的回溯?你是怎么打印chance的?请注意,print u"10"print 10 产生完全相同的输出。始终使用repr()(如print repr(chance))来诊断值。

标签: python int jython bukkit operand


【解决方案1】:

“它打印 10”并没有告诉你它是什么类型的。它可能是字符串"10" 而不是数字10——正如您可能从方法名称getString 中猜到的那样。您不能将字符串除以数字。尝试做:

chance = int(self.cfg.getString("main.candydropchance"))

【讨论】:

  • 它有效,但这不是偶然的,它就像它在配置中的 100%,而不仅仅是 10%?
  • @AmarKalabić:如果您使用的是 Python 2.x,您还应该将chancetotal 的计算更改为chancetotal = chance / 100.0 之类的值(注意小数点)。否则,除法将是整数除法。
  • @AmarKalabić:抱歉,我无法理解您的评论。你是什​​么意思“这不是机会”?它在配置中是 100% 的吗?
  • @BrenBarn 现在修复了它,但你能看看这个吗:stackoverflow.com/questions/19582817/… 谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-12-27
  • 2014-02-14
  • 2018-11-08
  • 2015-01-17
  • 2016-09-07
  • 2022-01-03
相关资源
最近更新 更多