第五次课堂笔记(20180410)

本节课讲解几道练习题。

练习一:输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数。

代码如下:

while 1:
    strings = input("Please inpur a string(quit will be exit):")
    alpha, dig, space, other = 0, 0, 0, 0
    if strings.strip() == "quit":
        exit(1)
    for i in strings:
        if i.isdigit():
            dig += 1
        elif i.isspace():
            space += 1
        elif i.isalpha():
            alpha += 1
        else:
            other += 1
    print("alpha = {0}".format(alpha))
    print("dig = {0}".format(dig))
    print("space = {0}".format(space))
    print("other = {0}".format(other))

执行后效果图片如下:

234017_jykj_3804867.png

练习二、ABCD乘9=DCBA,A=? B=? C=? D=? 

代码如下:

for A in [1]:
    for B in range(0, 10):
        for C in range(0, 10):
            for D in [9]:
                if ((1000* A + 100*B + 10*C + D)*9 == 1000*D + 100*C + 10*B + A ):
                    print("A = {0}".format(A))
                    print("B = {0}".format(B))
                    print("C = {0}".format(C))
                    print("D = {0}".format(D))
                    print("{0}{1}{2}{3}x9={3}{2}{1}{0}".format(A, B, C, D))

效果如下:

234249_jbR8_3804867.png

练习三:九宫格。

练习四:数字阶乘。

代码如下:

#整数阶乘
def rizi(n):
    result = n
    for i in range(1,n):
        result  *= i
    return result


num =int(input("输入一个整数: "))
result=rizi(num)
print("% d 的阶乘是:%d " % (num,result))

输入数据效果如下:

234710_h3Zx_3804867.png

编码问题。思路

'''
原有编码       ->      unicode编码    -> 目的编码
decode("utf-8")  解码  ->       unicode       ->  encode("gbk")  编码
'''

windows默认编码为 gbk编码。

国际编码一般为 utf-8

在写代码时注意调整编码问题。

注:python3 兼容编码。

 

转载于:https://my.oschina.net/u/3804867/blog/1794254

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值