我对编码还比较陌生,对我的家庭作业有点困惑。我希望打印的结果只保留两位小数,不能再多留一位。我一直在乱搞一些格式选项,并试图找到网上的答案,但无法理解我需要做什么,任何帮助是非常感谢,谢谢!你知道吗
#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.')
如果要输出2位数字:
输出:
快速阅读python3格式:https://pyformat.info/#number或此处:https://docs.python.org/3.1/library/string.html#format-examples
例如:
format
不是一个声明,也不是改变其参数的东西:它产生一个字符串,您需要将它包含在输出中。你知道吗相关问题 更多 >
编程相关推荐