习题22:更多的练习

发布时间:2017-11-14 17:01:35编辑:Run阅读(5191)

    练习代码如下

    # coding: utf-8
    __author__ = 'www.py3study.com'
    print("Let's practice everything.")
    print("You\'d need to know \'bout escapes with \\ that do \n newlines and \t tabs.")
    poem = """
    \tThe lovely world
    with logic so firmly planted
    cannot discern \n the needs of love
    nor comprehend passion from intuition
    and requires an explanation
    \n\t\twhere there is none.
    """
    print('-'*50)
    print(poem)
    print('-'*50)
    five = 10 - 2 + 3 - 6
    print("This should be five:{}".format(five))
    def secret_formula(started):
        jelly_beans = started * 500
        jars = jelly_beans / 1000
        crates = jars / 100
        return jelly_beans, jars, crates
    started_point = 10000
    print(secret_formula(started_point))
    beans, jars, crates = secret_formula(started_point)
    print("With a starting point of:{}".format(started_point))
    print("We'd have {} beans, {} jars, and {} crates.".format(beans, jars, crates))
    start_point = started_point / 10
    print("We can also do that this way:")
    print("We'd have %d beans, %d jars, and %d crates." % secret_formula(start_point))

    应该看到的结果

    图片.png


    常见问题

    为什么你在后面把jelly_beans这个变量名又叫成了beans?

    这是函数的工作原理,记住函数内部的变量都是临时的,当你的函数返回以后,返回值可以被赋予一个变量,这里是创建了一个新变量,用来存放函数的返回值

关键字