如何格式化打印语句以打印到一定数量的小数点?

时间:2014-05-02 23:53:06

标签: formatting python-3.3

我正在尝试在python 3.3中编写一个程序,这将使冰球守门员能够计算他的目标与平均值和节省百分比。 GAA通常以这种格式出现:

        1.50

并且保存百分比以下列格式显示:

        0.927

考虑到这一点,到目前为止我的代码是:

  Goals_Against = input("Enter Number of Goals Allowed: ")
  Games_Played = input("Enter Number of Games Played: ")
  Shots_Faced = input("Enter Number of Shots Faced: ")
  Shots_Saved = input("Enter Number of Shots Saved: ")      

  def GoalsAgainstAverage():
       GAA = (int(Goals_Against) / int(Games_Played))
       print("Your Goals Against Average is:",GAA)
       format(GAA, '.2f')
  def main():
      GoalsAgainstAverage()

  main()

输入后,例如,12作为Goals_Against的输入,8作为Games_Played的输入,它总是按以下格式打印:

  1.5

为什么不显示为1.50?

1 个答案:

答案 0 :(得分:0)

试试这个:

print("Your Goals Against Average is:", "{0:.2f}".format(GAA))