V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
tom1271
V2EX  ›  Python

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

  •  
  •   tom1271 · 2017-12-30 17:26:35 +08:00 · 917 次点击
    这是一个创建于 2280 天前的主题,其中的信息可能已经有所发展或是发生改变。
    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'
    目前尚无回复
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   955 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 21:48 · PVG 05:48 · LAX 14:48 · JFK 17:48
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.