为什么我会收到此错误“不支持的操作数类型”?

时间:2013-07-18 13:36:45

标签: python typeerror

def main():
    import time
    import random
    def basic():
     nl = "-----------------------Loading Please Wait--------------------"

     print (nl)
     print("wellcome to trade seas")
     print (nl)
     namE = raw_input("What is your name caption\n")
     nameS = namE + "'s"
     cap = "Caption "
     tion = " info"
     gold2 = str(500)
     gold1 = "\ngold --------- $"
     sails1 = "\nSails left ---"
     sails2 = 10
     print (nl)
     iL = ("\n~~~Caption here is our map~~~\nPort Royal --- 'a'\nFlorida Keys --- 'b'\nHavana --- 'c'")
     aportroyal = "a"
     bfloridakeys= "b"
     bhavana = "c"
     foodshoP = "b1"
     shipyarD = "b2"
     sailL = "b3"
     buildingL = ("\nFood Shop ---- b1\nShipYard\n")
     apples = 3
     bananas = 5
     foodlisT = ("\napples ---- f1\nbanana ---- f2")
     info = cap + str(nameS) + tion + gold1 + str(gold2) + sails1 + str(sails2)
     global info
     global gold2
     def Start():
                  global info
                  print (info)

                  print (iL)
                  print (nl)
                  sails = raw_input("What island would you like to sail to caption?\n")
                  taX = random.randint(30, 50)


                  global gold2

                  if aportroyal == sails:


                      print (nl)
                      print ("import tax of $", + taX,"\nWellcome to Port Royal")
                      ranG = random.randint(3, 4)

                      gold2 -= taX

                      info1 = cap + str(nameS) + tion + gold1 + str(gold2) + sails1 + str(sails2)
                      print(info1)
                  elif bfloridakeys == sails:

                      taX = random.randint(20, 38)
                      info = cap + str(nameS) + tion + gold1 + str(gold2) + sails1 + str(sails2)
                      print (info1)
                      gold2 -= taX
                      print (nl)
                      print ("import tax of $", + taX,"\nWellcome to Florida Keys")
                      ranG = random.randint(2, 3)

                  elif bhavana == sails:

                      taX = random.randint(2, 20)
                      gold2 -= taX
                      info = cap + str(nameS) + tion + gold1 + str(gold2) + sails1 + str(sails2)
                      print (info1)
                      print (nl)
                      print ("import tax of $", + taX,"\nWellcome to Florida Keys")
                      ranG = random.randint(1, 2)

                  else:
                   print (nl)
                  def prices():
                    bapples2 = apples * ranG
                    bapples1 = "bf1---apples ------ $"
                    bbananas2 = bananas * ranG
                    bbananas1 = "\nbf2---bananas ------ $"
                    sbananas2 = bananas / ranG
                    sbananas1 = "sf2-------banana ------ $"
                    sapples2 = apples / ranG
                    sapples1 ="\nsf1 ----apples ------ $"
                    buypL = bapples1 + str(bapples2) + bbananas1 + str(bbananas2)
                    bf1 = "bf1"
                    bf2 = "bf2"

                    def port():
                       print (buildingL)
                       print (nl)
                       buildinG = raw_input("what building would you like to go in caption\n")

                       if foodshoP == buildinG:
                           print ("wellcome to the food shop")
                           buysell = raw_input("hi catption 'buy' or 'sell'?\n")
                           buY = "buy"
                           selL = "sell"
                           gold2 = gold2
                           if buY == buysell:
                            print (buypL)                             
                            bbuy = raw_input("type food name to buy, or type anykey to return")
                            if bf1 == bbuy:

                            gold2 -= bapples
                             print (info2)
                             return port()
                            else:
                             return port()
                           else:
                            return port()


                       else:
                          return port()


                    port()
                  prices()  
     Start()
    basic()
main()

我在第52行得到错误

Unsupported operand type for -= str and int

1 个答案:

答案 0 :(得分:3)

您为gold2提供了一个字符串值:

gold2 = str(500)

但尝试减去整数:

gold2 -= taX

不要这样做:

>>> gold2 = str(500)
>>> gold2 -= 30
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for -=: 'str' and 'int'

也许你的意思是让gold2成为整数?