Python中的两位小数格式化

2024-09-29 21:20:34 发布

您现在位置:Python中文网/ 问答频道 /正文

我对编码还比较陌生,对我的家庭作业有点困惑。我希望打印的结果只保留两位小数,不能再多留一位。我一直在乱搞一些格式选项,并试图找到网上的答案,但无法理解我需要做什么,任何帮助是非常感谢,谢谢!你知道吗

 #Ingredients per 48 cookies
    sugar = 1.5
    butter = 1
    flour = 2.75

    #what percent of 48 are the ingredients
    sugar1 = (1.5/48)

    butter1 = (1/48)

    flour1 = (2.75/48)

    #ask for amount of cookies from user
    cookies = int (input('How many cookies would you like to bake? '))


    #calculate ingredient amounts
    sugar2 = (sugar1 * cookies)
    format(sugar2, '.2f')

    butter2 = (butter1 * cookies)
    format(butter2, '.2f')

    flour2 = (flour1 * cookies)
    format(flour2, '.2f')

    print ('To make', cookies, ' you need', sugar2, 'cups of sugar,',
           butter2, 'cups of butter, and', flour2, ' cups of flour.')

Tags: ofyouformat编码sugarcookiesbuttercups

热门问题