推荐学习书目
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 · Dec 30, 2017 · 1311 views
    This topic created in 3081 days ago, the information mentioned may be changed or developed.
    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'
    No Comments Yet
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   1199 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 23:44 · PVG 07:44 · LAX 16:44 · JFK 19:44
    ♥ Do have faith in what you're doing.