Python第二次课堂作业:统计单词个数、替换字符‘E‘为空格、根据身份证号输出出生日期

一、统计单词个数

# 第一题:编写程序,给出一个英文句子,统计单词个数。
print("The number of words is: ", len(input("Please input a sentence: ").split()))

在这里插入图片描述
二、替换字符‘E‘为空格

# 第二题:编写程序,给出一个字符串,将其中的字符 “E” 用空格替换后输出。
print(input("Please input a sentence: ").replace("E", " "))

在这里插入图片描述

三、根据身份证号输出出生日期

# 第三题:从键盘交互式输入一个人的18位的身份证号,以类似于"2001年09月12日"的形式输出该人的出生日期。
Id = input("Please input a ID number: ")
print((Id[6:10] + "年" + Id[10:12] + "月" + Id[12:14] + "日") if len(
    Id) == 18 else "Please input a valid ID number.")

在这里插入图片描述

四、三合一作业

# 作业名称:统计单词个数、替换字符'E'为空格、根据身份证号输出出生日期
# 作者:刘海
# 日期:2022-9-20

def oneDef():
    # 第一题:编写程序,给出一个英文句子,统计单词个数。
    print("The number of words is: ", len(input("Please input a sentence: ").split()))


def twoDef():
    # 第二题:编写程序,给出一个字符串,将其中的字符 “E” 用空格替换后输出。
    print(input("Please input a sentence: ").replace("E", " "))


def threeDef():
    # 第三题:从键盘交互式输入一个人的18位的身份证号,以类似于"2001年09月12日"的形式输出该人的出生日期。
    Id = input("Please input a ID number: ")
    print((Id[6:10] + "年" + Id[10:12] + "月" + Id[12:14] + "日") if len(
        Id) == 18 else "Please input a valid ID number.")


# 函数数组
defList = ["oneDef", "twoDef", "threeDef"]

while 1:
    # 让用户选择需要查看哪一道题目,查看完后再次询问是否继续查看,直到用户选择退出为止
    numbers = input("please input the number of the question you want to see(1-3),input 'q' to quit:")
    if numbers == 'q':
        print("Bye!")
        break
    elif numbers.isdigit() and int(numbers) in range(1, 4):
        # 新知识点:globals() 将访问全局变量、函数或类(可读写),locals() 将访问局部变量、函数或类(只读「其实是局部变量命名空间的拷贝」)
        globals()[defList[int(numbers) - 1]]()
    else:
        print("Please input a valid number.")

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值