python基础训练 day7

python基础训练 day7

小白获得累计打卡一周成就!

题目来源

上题:

第一题
# 利用条件运算符的嵌套来完成此题:学习成绩>=90分的同学用A表示,60-89分之间的用B表示,60分以下的用C表示。

看到此处,首先对条件运算符进行简单的回顾:

条件运算符回顾
条件运算符:
    < > >= <= == !=
    特别注意:
        == 对对象值进行比较 Value
        is , is not  对对象id的比较 ID
    返回一个布尔类型

然后,利用多重条件判断求解:

you_put = int(input('请输入你的成绩:'))
if 90 <= you_put <= 100:
    result = 'A'
elif 60 <= you_put <= 89:
    result = 'B'
elif 0 <= you_put <= 59:
    result = 'C'
else:
    result = '?'
print('你的成绩为{},获得{}'.format(you_put, result))

运行结果:

请输入你的成绩:66
你的成绩为66,获得B
第二题
# 输出指定格式的日期。使用 datetime 模块。

没怎么用过datetime块,照着敲一遍,学习一下。

import datetime

if __name__ == '__main__':
    # 输出今日日期,格式为 dd/mm/yyyy。更多选项可以查看 strftime() 方法
    print(datetime.date.today().strftime('%d/%m/%Y'))   # strftime()格式化方法,之前有总结过
    # 创建日期对象
    china_birthday = datetime.date(1949, 10, 1)
    print(china_birthday.strftime('%d/%m/%Y'))
    # 日期算术运算
    china_birthday_next_day = china_birthday + datetime.timedelta(days=1)
    print(china_birthday_next_day.strftime('%d/%m/%Y'))
    # 日期替换
    china_first_birthday = china_birthday.replace(year=china_birthday.year + 1)
    print(china_first_birthday.strftime('%d/%m/%Y'))

运行结果:

26/11/2021
01/10/1949
02/10/1949
01/10/1950
第三题
# 输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数。

对于字符串,有如下检验方法

字符串的判别相关函数
str.isdigit()	# 是否为数字
str.isalpha()	# 是否为字母
str.isspace()	# 是否为空格符号
str.isascii()	# 是否为ASCII码
str.isalnum()	# 是否为字母或数字

根据以上函数,套用一下

str1 = input('请输入一串字符')
num = 0
eng = 0
space = 0
other = 0
for i in str1:
    if i.isdigit():
        num += 1
    elif i.isalpha():
        eng += 1
    elif i.isspace():
        space += 1
    else:
        other += 1
print('英文字母{}个,数字{}个,空格{}个,其他字符{}个'.format(eng, num, space, other))

在键盘上胡乱敲打了一番:

# 运行结果
请输入一串字符af af16af516v1 af
英文字母9个,数字6个,空格2个,其他字符0个

第七天打卡结束!

雄关漫道真如铁,而今迈步从头越

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值