day07 ,08 元组,集合,字典

day07 ,08 元组,集合,字典

一,练习

  1. 输入用户名,判断用户名是否合法(用户名必须包含且只能包含数字和字母,并且第一个字符必须是大写字母)

    例如: ‘abc’ — 不合法 ‘123’ — 不合法 ‘abc123’ — 不合法 ‘Abc123ahs’ — 合法

    name=input("请输入一个用户名:")
    count=0
    if "A"<=name[0]<="Z" and len(name)>1:
        for i in name[1:]:
            if "0"<=i<="9":
                count+=1
            elif "a"<=i<="z"or"A"<=i<="Z":
                continue
            else:
                print("用户名非法") 
                break
        else:
            result="合法" if count !=0 else"非法"
            print(result)
    else:
        print("用户名非法")
    
  2. 写程序实现字符串函数find/index的功能(获取字符串1中字符串2第一次出现的位置)

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

    str1="how are you? Im fine, Thank you!"
    str2="you"
    if str2 in str1:
        for i in range(0,len(str1)-len(str2)+1):     # 防止下标越界产生,
            if str1[i:i+len(str2)]==str2:
                print(i)
                break
    else:
        print("字符串2不在字符串一中")
    
  3. 生成50-300之间可重复的 10个数据 存放于列表中, 保证列表中元素的顺序,对列表进行排重,并对列表使用排序算法进行降序排序

    例如:随机生成了[70, 88, 91, 70, 107, 234, 91, 177, 282, 197]
    		--- 去重之后 [70, 88, 91, 107, 234, 177, 282, 197]
      	---- 降序排序 [282, 234, 197, 177, 107, 91, 88, 70]
    
    # 生成10个任意数据
    import random
    list1=[random.randint(50,300) for _ in range(10)]
    print(list1)
    # 去重
    list2=[]
    for i in list1:
        if i not in list2:
            list2.append(i)
    print(list2)
    # 排序
    for i in range(1,len(list2)):
        for j in range(0
  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值