笔记28 笨办法学python,自己找到的几个练习,就当作ex37来做

笔记28 笨办法学python,自己找到的几个练习,就当作ex37来做

教材让我们自找代码,我找到一本网上下载的python百题练习集。立刻开始第一个练习,做成了ex37.py。做得很顺利,做完后想到和for类似的while,但把这个while套用到这个用for的代码上,试了无数次都不行。看来这两个指示符还只是近似,差别也许很大,只有在继续中理解。Ex37的扩展代码应该可以做得好一些,我在给出最初的小代码之后,再把对练习37的扩展也做一点出来。
这是最简ex37.py,然后把它缩小成ex37.0py,也能执行。

在这里插入代码片`在这里插入代码片`or i in range(1, 5):
    for j in range(1, 5):
        for k in range(1, 5):
            if(i != k) and (i != j) and (j != k):
                 print (i, j, k)

执行结果在这里插入代码片PS C:\Users\lenovo\1pythonw> python ex37.py 1 2 3 1 2 4 1 3 2 1 3 4 1 4 2 1 4 3 2 1 3 2 1 4 2 3 1 2 3 4 2 4 1 2 4 3 3 1 2 3 1 4 3 2 1 3 2 4 3 4 1 3 4 2 4 1 2 4 1 3 4 2 1 4 2 3 4 3 1 4 3 2

把这个练习缩小一部分成为ex37.0.py,也很顺利。
练习ex37.0.py在这里插入代码片for i in range(2, 6):
for j in range(2, 6):
if(i != j):
print (i, j)

执行结果如下:

在这里插入代码片PS C:\Users\lenovo\1pythonw> python ex37
2 3
2 4
2 5
3 2
3 4
3 5
4 2
4 3
4 5
5 2
5 3
5 4
PS C:\Users\lenovo\1pythonw>

扩展的ex37.py也许能够顺利一点。把那个ex37.1弄得没有语法错误,可以执行的时候,结果让人哭笑不得,不是计算的结果,而是跑题后的结果。新手的尴尬也!
这是ex37.1的代码和执行结果,能够顺利执行,这是出乎意料之外的。

在这里插入代码片Range = [1, 2, 3, 4, 5]

while True:
        choice = input("> ")

        if choice == "2" or "3":
            print("do you choice which number?")
        else:
            print("i have no idea")

执行结果在这里插入代码片PS C:\Users\lenovo\1pythonw> pyth

2
do you choice which number?
3
do you choice which number?
5
do you choice which number?

接着又结合练习35弄出来一个ex37.2.py,没有想到很顺利,也能够顺利执行,但如果不是回答ok和no的情况,又出现name error名称错误。这个练习暂且到此,继续去看练习38吧。
这是ex37.2.py的代码和执行结果,作为笔记留存。

在这里插入代码片def begining():
    print("here you will see a difficult question.")
    print("there is a test that is waiting for  you.")
    print("Would you recievce the test?")

    answer = input("> ")

    if answer == "ok":
        range(1, 5)
    elif answer == "no":
        print("you can go through it!")
    else:
        dead("you are badly!")

begining()

for i in range(1, 5):
    for j in range(1, 5):
        for k in range(1, 5):
            if(i != k) and (i != j) and (j != k):
                 print (i, j, k)

执行结果`PS C:\Users\lenovo> cd 1pythonw
PS C:\Users\lenovo\1pythonw> python ex37.2.py
here you will see a difficult question.
there is a test that is waiting for you.
Would you recievce the test?

ok
1 2 3
1 2 4
1 3 2
1 3 4
1 4 2
1 4 3
2 1 3
2 1 4
2 3 1
2 3 4
2 4 1
2 4 3
3 1 2
3 1 4
3 2 1
3 2 4
3 4 1
3 4 2
4 1 2
4 1 3
4 2 1
4 2 3
4 3 1
4 3 2
PS C:\Users\lenovo\1pythonw> python ex37.2.py
here you will see a difficult question.
there is a test that is waiting for you.
Would you recievce the test?
no
you can go through it!
1 2 3
1 2 4
1 3 2
1 3 4
1 4 2
1 4 3
2 1 3
2 1 4
2 3 1
2 3 4
2 4 1
2 4 3
3 1 2
3 1 4
3 2 1
3 2 4
3 4 1
3 4 2
4 1 2
4 1 3
4 2 1
4 2 3
4 3 1
4 3 2
PS C:\Users\lenovo\1pythonw> python ex37.2.py在这里插入代码片`

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值