【问题标题】:How do I get periods to display behind the index numbers?如何在索引号后面显示句点?
【发布时间】:2021-07-27 20:58:06
【问题描述】:

我正在为学校做一个项目。我能够让我的函数按照我需要的方式工作,使用老师想要的所有参数,除了它显示的时候。我需要它从两个列表中提取,它确实如此,并将两个列表彼此相邻显示,它确实如此。但是在教师版中,显示/输出如下所示:

0.  ($2.39)   Drip coffee
1.  ($3.59)   Hot chocolate
2.  ($3.79)   Caffe Latte
3.  ($1.49)   Bagel
4.  ($2.69)   Blueberry muffin
5.  ($2.39)   Chocolate chip cookie

6. Reset order
7. Checkout

但是,我的版本是这样的:

0  ($2.39)   Drip coffee
1  ($3.59)   Hot chocolate
2  ($3.79)   Caffe Latte
3  ($1.49)   Bagel
4  ($2.69)   Blueberry muffin
5  ($2.39)   Chocolate chip cookie

6. Reset order
7. Checkout

如您所见,我错过了索引号后面的句点(抱歉,如果我的术语不准确。我是一个极端的初学者,我很难保持这些术语的正确性。)。这是我的代码:

products = [    "Drip coffee",
                "Hot chocolate",
                "Caffe Latte",
                "Bagel",
                "Blueberry muffin",
                "Chocolate chip cookie" ]

prices = [      2.39,
                3.59,
                3.79,
                1.49,
                2.69,
                2.39 ]

for i, product in enumerate(products):
  price = prices[i]
  print(i, "(${})".format(price), product)
print()
print("6. Reset", "\n" + "7. Checkout")

有什么方法可以添加或修复它,以便输出将在我需要的地方显示句点?此外,该项目的说明说将 6 和 7 的选项作为字符串放在 for 循环之外。这就是为什么他们不像其他人一样被包括在内。这也是为什么我很容易在最后两个数字后面加上句号的原因,哈哈。任何帮助以及对解决方案为何如此的解释都将不胜感激。谢谢大家!

【问题讨论】:

    标签: python for-loop indexing format period


    【解决方案1】:

    为了打印句点,当然可以告诉 python 打印句点!

    print("{}.".format(i), "(${})".format(price), product)
    

    但是,这是一个非常时髦的打印声明。让我们探索一些其他选项:

    # a bit more readable using format
    print("{}. (${}) {}".format(i, price, product))
    # using the old-style string formatting
    print("%d. ($%.2f) %s" % (i, price, product))
    # using new-style f-strings
    print(f"{i}. (${price:.2f}) {product}")
    

    供参考,old-style string formattingf-strings。另外,这里有一个用于字符串格式化的cheat sheet。我使用.2f 表示精度为2 的定点数,例如2.5 打印为2.502.504234 也打印为2.50

    另外,让我们使用zip 让您的循环更加pythonic:

    for i, (prod, price) in enumerate(zip(products, prices)):
       print(f"{i}. (${price:.2f}) {prod}")
    

    干净多了,不是吗?学习愉快!

    【讨论】:

      【解决方案2】:

      您应该标记语言,但这会做到:

      products = [    "Drip coffee",
                      "Hot chocolate",
                      "Caffe Latte",
                      "Bagel",
                      "Blueberry muffin",
                      "Chocolate chip cookie" ]
      
      prices = [      2.39,
                      3.59,
                      3.79,
                      1.49,
                      2.69,
                      2.39 ]
      
      for i, product in enumerate(products):
        price = prices[i]
        print("{}. (${})".format(i, price), product)
      print()
      print("6. Reset", "\n" + "7. Checkout")
      

      “,”允许您添加另一个变量,但也可以添加一个空格。只需将其更改为 + 并添加“。”即可,就像我在上面所做的那样,使用 .format 放置两个变量。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-01-02
        • 2016-08-23
        • 2019-04-28
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多