day5 字典、字符串作业

  1. 输入一个字符串,打印所有奇数位上的字符(下标是1,3,5,7…位上的字符)

例如: 输入**'abcd1234 ' ** 输出**'bd24'**

str_1 = input("请输入一个字符串:")
str_2 = ""
for x in range(1,len(str_1),2):
    str_2 += str_1[x]
print(str_2)
  1. 输入用户名,判断用户名是否合法(用户名长度6~10位)

name = input("请输入用户名")
if 6 <= len(name) <=10:
    print("用户名合法")
else:
    print("用户名不合法")
  1. 输入用户名,判断用户名是否合法(用户名中只能由数字和字母组成)

例如: 'abc' — 合法 '123' — 合法 ‘abc123a’ — 合法

name = input("请输入用户名:")
count = 0
for x in name:
    if "a" <= x <= "z" or "A" <= x <= "Z" or "0" <= x <= "9":
        count += 1
    else:
        continue
if count == len(name):
    print("合法")
else:
    print("不合法")
  1. 输入用户名,判断用户名是否合法(用户名必须包含且只能包含数字和字母,并且第一个字符必须是大写字母)

例如: 'abc' — 不合法 '123' — 不合法 'abc123' — 不合法 'Abc123ahs' — 合法 'Abc' — 不合法

name = input("请输入用户名:")
count = 0
if "A" <= name[0] <= "Z":
    count += 1
    count_1 = 0
    for x in range(1 , len(name)):
        if "a" <= name[x] <= "z" or "A" <= name[x] <= "Z" or "0" <= name[x] <= "9":
            count_1 += 1
        else:
            print("不合法")
            break
    if count_1 == len(name)-1:
        count += 1
    count_2 = 0
    for i in name:
        if "0" <= i <="9":
            count_2 =1
        else:
            continue
    if count_2 == 1:
        count += 1
    else:
        print("不合法")
else:
    print("不合法")
  1. 输入一个字符串,将字符串中所有的大写字母取出来产生一个新的字符串

例如:输入**'abFc1shj2A3klBs99+2kDkk'** 输出:**'FABD'**

str_5 = input("请输入一个字符串:")
new_str = ''
for x in str_5:
    if "A" <= x <= "Z":
        new_str += x
print(new_str)
  1. 输入字符串,将字符串的开头和结尾变成'+',产生一个新的字符串

例如: 输入字符串**'abc123', 输出'+bc12+'**

str_6 = input("请输入字符串:")
new_str = '+'
for i in range(1,len(str_6)-1):
    new_str += str_6[i]
new_str += "+"
print(new_str)
  1. 输入字符串,获取字符串的中间字符

例如: 输入**'abc1234'** 输出:'1' 输入**'abc123'** 输出**'c1'**

str_7 = input("请输入字符串:")
if len(str_7) % 2 == 0:
    print(str_7[len(str_7)//2 - 1], str_7[len(str_7)//2])
else:
    print(str_7[len(str_7)//2])
  1. (难)写程序实现字符串函数find/index的功能(获取字符串1中字符串2第一次出现的位置)

例如: 字符串1为:how are you? Im fine, Thank you! , 字符串2为:you, 打印8

str_1 = "how are you? Im fine, Thank you!"
str_2 = "you"
len_str_1 = len(str_1)
len_str_2 = len(str_2)
for i in range(len_str_1):
    if str_1[i] == str_2[0]:
        count = 0
        for x in range(len_str_2):
            if str_1[i+x] == str_2[x]:
                count += 1
            else:
                continue
        if count == len_str_2:
            print(i)
            break
        else:
            continue
  1. 获取两个字符串中公共的字符

例如: 字符串1为:abcaaa123, 字符串2为: huak3 , 打印:公共字符有:a3

str_1 = "abcaaa123"
str_2 = "huak3"
new_str =  ""
for x in str_1:
    if x in str_2 and x not in new_str:
        new_str += x
print(new_str)
  1. 定义一个变量保存一个学生的信息,学生信心中包括:姓名、年龄、成绩(单科)、电话、性别

student_1 = {"name": "tian", "age": 21, "score_math": 89, "phone": "15912047853", "gender": "男"}
  1. 定义一个列表,在列表中保存6个学生的信息(学生信息中包括: 姓名、年龄、成绩(单科)、电话、性别(男、女、不明) )

students = [
           {"name": "tian", "age": 21, "score_math": 89, "phone": "1591205653", "gender": "男"},
           {"name": "ming", "age": 15, "score_math": 56, "phone": "15923247853", "gender": "女"},
           {"name": "wang", "age": 19, "score_math": 45, "phone": "159122147853", "gender": "男"},
           {"name": "zhang", "age": 14, "score_math": 99, "phone": "15921047853", "gender": "男"},
           {"name": "li", "age": 28, "score_math": 76, "phone": "15912347853", "gender": "不明"},
           {"name": "du", "age": 55, "score_math": 32, "phone": "15916779953", "gender": "女"}
           ]
  1. 统计不及格学生的个数

count_score = 0
for student in students:
    if student["score_math"] < 60:
        count_score += 1
print(count_score)	# 3
  1. 打印不及格未成年学生的名字和对应的成绩

for student in students:
    if student["score_math"] < 60 and student["age"] < 18:
        print(student["name"],student["score_math"]) # ming 56
  1. 求所有男生的平均年龄

count_age_sum = 0
count_gender_len = 0
for student in students:
    if student["gender"] == "男":
        count_gender_len += 1
        count_age_sum += student["age"]
age_avg = count_age_sum / count_gender_len
print(age_avg)	# 18.0
  1. 打印手机尾号是8的学生的名字

for student in students:
    if student["phone"][-1] == "8":
        print(student["name"])
# zhang
# li
  1. 打印最高分和对应的学生的名字

score_math = []
for student in students:
    score_math.append(student["score_math"])
score_max = max(score_math)
for student in students:
    if student["score_math"] == score_max:
        print(student["name"],student["score_math"]) # zhang 99
  1. 删除性别不明的所有学生

new_students = []
for i in range(len(students)):
    if students[i]["gender"] == "不明":
        continue
    else:
        new_students.append(students[i])
print(new_students)
  1. 将列表按学生成绩从大到小排序(挣扎一下,不行就放弃)

score_math_1 = []
for student in students:
    score_math_1.append(student["score_math"])
score_math_1.sort(reverse=True)
new_students_1 = []
for i in range(len(score_math_1)):
    for j in range(len(students)):
        if students[j]["score_math"] == score_math_1[i]:
            new_students_1.append(students[j])
        else:
            continue
print(new_students_1)

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值