Python 字典字符串格式

Python 字典字符串格式,python,string,dictionary,formatting,Python,String,Dictionary,Formatting,用户将在原始输入中键入他们想要出售的商品,并根据他们的库存进行检查,如果在他们的库存中,则现在将检查它是否在Pickaxes字典中,如果是,则我想打印一些内容,告诉他们从字典中的值中获取该商品的金额 例如,用户在原始输入中键入青铜鹤嘴锄,我希望打印输出为: print(“对于项目%s,您将收到%s硬币”)%(在原始输入中选择的鹤嘴锄名称)(以及字典中的售价)。您可以尝试 Pickaxes = { 'adamant pickaxe': {'name': 'adamant pickaxe', 'c

用户将在原始输入中键入他们想要出售的商品,并根据他们的库存进行检查,如果在他们的库存中,则现在将检查它是否在Pickaxes字典中,如果是,则我想打印一些内容,告诉他们从字典中的值中获取该商品的金额

例如,用户在原始输入中键入青铜鹤嘴锄,我希望打印输出为:
print(“对于项目%s,您将收到%s硬币”)%(在原始输入中选择的鹤嘴锄名称)(以及字典中的售价)。
您可以尝试

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()  
在与代码集成时:

>>> 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 ##
在任何情况下,您都应该使用全新的
格式
方法,而不是旧式和旧式格式

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

尝试以下方法,快速、肮脏地破解您的代码:

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

整洁的项目想法:-)你有问题吗?是的,我如何使用原始输入中的输入设置字典中字符串的格式,以确定要使用的值如果你将此添加到我的代码中,它将如何编写:我发现这有点让人困惑,但我知道这就是我想要的want@Ali,请看一下修改。我已经试过了,我还是不明白,你能把它融入我的代码吗。对不起,麻烦了,非常感谢你的回答是的,凯文,这是我的想法,但我想从我的字典中格式化,我还想通过用户在原始输入中键入的内容识别售价,然后打印出来。我不确定你要的是什么。你已经可以使用Pickaxes[selling][“sell”]访问价格。是的,这对我来说不适用于该打印内容。我只想知道如何正确编写格式正确的打印语句等。这正是我想要的!非常感谢,我知道这可能不是你想要做的最好的方式。下面的问题还有几个很好的答案。这将使您的代码在不进行重构的情况下工作,但一旦它工作了,就可以做得更好。
>>> print("For the item {} you will receive {} Coins".format(name,price))
For the item diamond pick you will receive 999 Coins
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()