字典字符串格式

时间:2013-12-24 17:41:24

标签: python string dictionary formatting

Pickaxes = {
 'adamant pickaxe': {'name': 'adamant pickaxe', 'cost': 150, 'sell': 100},
 'bronze pickaxe': {'name': 'bronze pickaxe', 'cost': 10, 'sell': 1},
 'dragon pickaxe': {'name': 'dragon pickaxe', 'cost': 666, 'sell': 333}}


def sell():
   global sell
   global money
   print "Your inventory", inventory
   selling = raw_input("\nWhich item in your inventory would you like to sell?: \n")
   if selling in inventory:
        if selling in Pickaxes:
           print "You have chosen the item", selling
           print "In return you will recieve %(sell)% Coins" % Pickaxes[selling]
           confirm = raw_input ("\nAre you sure you wish to sell the item\n")
           if confirm == "yes":
                i = inventory.index(selling)
                del inventory[i]
                money = money + Pickaxes[selling]["sell"]
                print "You now have", money, "Coins"
                time.sleep(2)
                raw_input("\nHit Enter to return to the menu\n")
                home()  

用户将在raw_input中键入他们想要销售的内容,并将根据他们的库存进行检查,如果在库存中它现在将检查它是否在Pickaxes DICTIONARY中,如果是,那么我想打印一些内容会告诉他们从字典中的值中获取该项目的数量。

例如,用户在raw_input中键入bronze pickaxe,我希望打印输出为: print("For the item %s you will receive %s Coins") % (The name of the pickaxe chosen in the raw_input)(And the sell price within the dictionary).

3 个答案:

答案 0 :(得分:1)

听起来您正在尝试格式化您的打印​​语句,但是您收到了错误。

>>> name = "diamond pick"
>>> price = 999
>>> print("For the item %s you will receive %s Coins") % (name)(price)
For the item %s you will receive %s Coins
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'str' object is not callable

你的语法有点偏。 %应该在 print的括号内,右侧应该是单个元组。

>>> print("For the item %s you will receive %s Coins"% (name,price))
For the item diamond pick you will receive 999 Coins

在任何情况下,你应该使用闪亮的新format方法与旧的和尘土飞扬的百分比样式格式相比。

>>> print("For the item {} you will receive {} Coins".format(name,price))
For the item diamond pick you will receive 999 Coins

答案 1 :(得分:0)

你可以尝试

>>> stmt = "For the item {0} you will receive %(sell)s Coins"

>>> raw_input("{0} {1}".format((stmt % Pickaxes[selling]).format(selling),
              'Please Confirm: (y/n):') )

For the item adamant pickaxe you will receive 100 Coins Please Confirm: (y/n): 

集成到代码中时:

  ## Your code before the if condition ##
  if selling in Pickaxes:

       stmt = "For the item {0} you will receive %(sell)s Coins"

       confirm = raw_input("{0} {1}".format((stmt % Pickaxes[selling]).format(selling),
                           "Please Confirm: (y/n):"))

       if confirm.lower().strip() == "y":
           ## rest of your code ##

答案 2 :(得分:0)

尝试使用此代码来快速破解代码:

if selling in Pickaxes:

       confirm = raw_input ("Are you sure you wish to sell the item {} "
                 "for {} Coins? (y/n)\n>>".format(
                     selling,Pickaxes[selling]["sell"]))
       if confirm.lower() in ["yes","y"]:
            i = inventory.index(selling)
            del inventory[i]
            money = money + Pickaxes[selling]["sell"]
            print "You now have", money, "Coins"
            time.sleep(2)
            raw_input("\nHit Enter to return to the menu\n")
            home()