各位前辈和老师帮忙做一下这个作业题,很变态找不到任何思路。谢谢!

2017-12-30 17:26:35 +08:00
 tom1271
1. A function that formats a positive decimal number e.g. 12.345 into a string representation of the number with a specified number of digits after the decimal point e.g. “ 12.35 ”. This function is useful in generating report for customer where you don ’ t want to present all the digits after the decimal point – as these are unnecessarily precision e.g. $12.345 is not much different from $12.346.

The following is the interface (also called, application program interface) of this function:

#*************************************************************************
# format(x, digits)
# format the input number x with the specified number of digits after the
# decimal point.
#
# inputs:
# x: a positive number in float or integer
# digits: number of digits after the decimal point
#
# returns: a string representation of the input number x rounded to the
# specified number of digits after the decimal point.
#
# example:
# >>> format(12.34,1)
# >>> 12.3
# >>> format(12.345, 2)
# >>> 12.35
# >>> format(12.99,1)
# >>> 13.0
#*************************************************************************
For this assignment you canNOT use Python ’ s String format() functions or the function round(). This exercise is design to give you the opportunity to think about String formatting. For future assignment, you may (and should) use these built-in functions whenever possible.
Hint. You may need to use the following “ in-line ” if-statement.
>>> a = 3
>>> "a is greater than 3" if a>3 else "a is not greater than 3"
'a is not greater than 3'
>>> "a is greater than or equal to 3" if a>=3 else "a is less than 3"
'a is greater than or equal to 3'
>>> "a is equal to 3" if a==3 else "a is not equal to 3"
'a is equal to 3'
>>> "a is 3" if a==3 else ("a greater than 3" if a>3 else "a less than 3")
'a is 3'
>>> a = 2
>>> "a is 3" if a==3 else ("a greater than 3" if a>3 else "a less than 3")
'a less than 3'
>>> a = 4
>>> "a is 3" if a==3 else ("a greater than 3" if a>3 else "a less than 3")
'a greater than 3'
932 次点击
所在节点    Python
0 条回复

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/418884

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX