笔记15 笨办法中的ex24.py一首诗和一个计算

笔记15 笨办法中的ex24.py一首诗和一个计算

教材中的ex23还是没有打开,不知是终端的问题还是代码的问题。总不能停在那里不动,了解了底层字节串概念和另一层面的代码字符串概念之后,继续前行,今天开始ex24。
还是有代码打错的问题,但已经很容易查错了。今天还发现另外一个错误,目录指引的错误,也是很快就发现了。看来有了认真,什么困难都能克服。
很快就执行了这个练习24,好像是两个互不相干的东西放在一个文件中。一个是一首诗,好像还不错的一首诗,我试着意译如下:
“这个可爱的世界
这么坚定地植入了‘逻辑’的一个世界
不可能不关心‘爱’的诉求
也不可能不去领会那来自直觉的‘情感’
并且还要求对这个情感给出说明和理由。

另一个则是一个计算,基本思路大概是这样的。
有一个给出五倍的秘密公式,然后有一个起点数字(start_point)10000,数字都涉及到豆子(beans),有三个持有豆子的客体:jelly,jar和crate。
Jelly持有的豆子起点数字乘以500,自然那就是5000000;
Jars持有的豆子是jelly数字除以1000,自然就是500;
Crates持有的豆子是jars数字除以100,自然只有5。
练习ex24的代码

在这里插入代码片print("Let's practice everything.")
print('You\'d need to know \'bout escapes with \\ that do:')
print('\n newlines and  \t tabs.')

poem = """
\t The 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}")

def secret_formula(started):
    jelly_beans = started * 500
    jars = jelly_beans / 1000
    crates = jars / 100
    return jelly_beans, jars, crates


start_point = 10000
beans, jars, crates = secret_formula(start_point)

# remember 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)
# this is an easy way to apply a list to a format string
print("We'd have {} beans, {} jars, and  {} crates.".format(*formula))

代码执行后的结果

在这里插入代码片PS C:\Users\lenovo\1pythonw> 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.

我把代码ex24的头三行尝试着做了交互式改造,做成ex24.1.py。觉得没有把握,谁知在终端一次成功。但对于return指令,好像还得加深理解。
改造后的ex24.1.py

在这里插入代码片"""# 交互式在终端打入起首话语
Firstline = input("one:")
secline = input("two:")
trdline = input("three:")

# 这首诗的内容一共分为五行
poem = """
\t The 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 - 5
print(f"This should be five: {five}")

def secret_formula(started):
    jelly_beans = started * 500
    jars = jelly_beans / 1000
    crates = jars / 100
    return jelly_beans, jars, crates


start_point = 10000
beans, jars, crates = secret_formula(start_point)

# remember 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)
# this is an easy way to apply a list to a format string
print("We'd have {} beans, {} jars, and  {} crates.".format(*formula))


``
在终端的运行结果:

```python
在这里插入代码片PS C:\Users\lenovo\1pythonw> python ex24.1.py
one:Let's practice everything.
two:You'd need to know'bout escape with \ that do:
three:newline 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.
PS C:\Users\lenovo\1pythonw>




2020/04/16

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值