Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/318.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 无法连接';str';和';int';对象(铁路超高总和)_Python_Python 2.7 - Fatal编程技术网

Python 无法连接';str';和';int';对象(铁路超高总和)

Python 无法连接';str';和';int';对象(铁路超高总和),python,python-2.7,Python,Python 2.7,我是初学者。我只想问一下为什么在输出中显示错误时出错。有什么建议吗?非常感谢您的帮助。下面是我的代码 class Player: def getchips(self): return self.chips def setchips(self, add): self.chips = self.chips + add class result(): if totalplayer > opponenttotal: print "Y

我是初学者。我只想问一下为什么在输出中显示错误时出错。有什么建议吗?非常感谢您的帮助。下面是我的代码

class Player:
  def getchips(self):
      return self.chips
  def setchips(self, add):
      self.chips = self.chips + add

class result():
      if totalplayer > opponenttotal:
          print "You have " + str(totalplayer)
          print self.opponent.getname() + " has " + str(opponenttotal)
          print "You win!"
          self.humanplayer.setchips(bet*2)
          self.humanplayer.setscore()
          self.replay()
class Game:
   def __init__(self):
      self.deck = Deck()
      self.humanplayer = Player()
输出:

self.chips=self.chips+add TypeError:无法连接'str'和'str' “int”对象


您的问题几乎可以肯定是您无意中将
self.chips
设置为字符串,可能是
self.chips=“100”
,而您可能需要的是
self.chips=100

除非你发布完整的代码,否则我们只能猜测

如果不是这样,那么

self.humanplayer.setchips(bet*2)
这就导致了你所犯的错误

self.chips = self.chips + add
如果
bet
是一个字符串,这是
输入(“下注”)
的默认结果,那么
bet*2
仍然是一个字符串,然后
self.chips+add
将字符串添加到筹码中,这毫无意义


无论您在哪里下注,都需要通过
int()

尝试将其转换为int:
self.chips=self.chips+str(add)
感谢您的帮助,但当我将add更改为str(add)时,它会给我以下错误-->输出:self.chips=self.chips+str(add)TypeError:unsupported operation type(s)对于+:'int'和'str'您不显示设置的位置
self.chips
。您希望此值是字符串还是数字?了解有关创建的详细信息。你的代码片段离完整的示例还差得很远。现在没关系了,我已经解决了这个问题。谢谢你的帮助:)