一、统计单词个数
# 第一题:编写程序,给出一个英文句子,统计单词个数。
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.")