Published 9月 04, 2018 by with 0 comment

習題24 - 更多的練習



用Notepad++打下列的程式碼,
另存為ex24.py. 我附上中文注釋方便好讀.
print ("Let's practice everything.")
print ('You\'d need to know \'bout escapes with \\ that do:')
print ('\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 ("------------")
print (poem)
print ("------------")

five = 10 - 2 + 3 - 6
print (f"this should be five: {five}")

# 定義一個函數叫 secret_formula, 它需要一個started參數
# 經運算後返回三個值 jelly_beans, jars, crates
def secret_formula (started):
    jelly_beans = started * 500
    jars = jelly_beans / 1000
    crates = jars / 100
    return jelly_beans, jars, crates

# 定義參數start_point為1000, 
start_point = 10000
print ("<<<<<<<<<<<<<<"beans, jars, crates)
print ("<<<<<<<<<<<<<<"jelly_beans, jars, crates)
# 這邊很有趣, 用beans 取代 jelly_beans
beans, jars, crates = secret_formula (start_point)

#下面就是各種格式化的方式.
# remembet that this is another way to format a string
print ("With a starting point of: {}".format (start_point))
# it's just like with an f"" string
print (f"We'd have {beans} beans, {jars} jars, and {crates} crates.")

start_point = start_point / 10

print ("We can also do that this way:")
# 用formula 來取代secret_formula (start_point) 之後產生的三個值, 這也是好方式.
formula = secret_formula (start_point)
# this is an easy way to apply a list to a format string
print ("We'd have {} beans, {} jars, and {} crates.".format (*formula))

然後用Windows的cmd, 執行python打開它.
C:\Windows\System32>cd C:\Users\Peter\Desktop\Python\LP3THW
C:\Users\Peter\Desktop\Python\LP3THW>python ex24.py
Let's practice everything.
You'd need to know 'bout escapes with \ that do:

 newlines and    tabs.
------------

        The lovely world
with logic so firmly planted
cannot discern
 the needs of love
nor comprehend passion from intuition
and requires an explanation

                where there is none.

------------
this should be five: 5
With a starting point of: 10000
We'd have 5000000 beans, 5000.0 jars, and 50.0 crates.
We can also do that this way:
We'd have 500000.0 beans, 500.0 jars, and 5.0 crates.

C:\Users\Peter\Desktop\Python\LP3THW>

完成

最初發表 / 最後更新: 2018.09.04 / 2018.09.04

0 comments:

張貼留言