【问题标题】:Adding a "%" at the end of a print statement [duplicate]在打印语句的末尾添加“%”[重复]
【发布时间】:2016-06-08 19:50:38
【问题描述】:

我有一个问题。我的一项任务是编写一个程序,输入 3 个测试分数并获得平均值,然后平均值应紧跟一个“%”符号。我不太确定在打印语句中添加“%”符号的位置。我翻遍了我的书,找不到任何东西。

keep_going = "y"

while keep_going == "y":

    test_1 = int(input(" Enter a test score: "))
    test_2 = int(input(" Enter a test score: "))
    test_3 = int(input(" Enter a test score: "))

    score = (test_1 + test_2 + test_3) / 3.0

    print(" Score ", format(score, ".3f",), sep="")

    keep_going = input(" Would you like to continue? (Enter y for yes)")

【问题讨论】:

  • 你想多了,最好的办法是print("Score {:.3f}%".format(score))
  • 有关字符串格式的更多信息,请参阅pyformat.info
  • “我不太确定在打印语句中的何处添加“%”符号。” 我有一个建议。当您对某事不太确定时,请猜测一下,看看它是否有效。编程的一大优点是,仅仅通过玩耍很难永久性地损坏任何东西。

标签: python


【解决方案1】:

这对我有用:

print(" Score ", format(score, ".3f",) + "%")

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-02-16
    • 1970-01-01
    • 2018-02-17
    • 2017-02-28
    • 1970-01-01
    • 1970-01-01
    • 2019-06-01
    相关资源
    最近更新 更多