zoukankan      html  css  js  c++  java
  • python习题

    #习题1
    #a:实现用户输入用户名和密码,当用户名为seven且密码为123时,显示登录成功,否则登录失败!
    while 1:
        name = input('请输入用户名: ')
        psw = input('请输入密码: ')
        if name == 'seven' and psw == '123':
            print('登陆成功')
            break
        else:
            print('登陆失败')
            break
    #b实现用户输入用户名和密码,当用户名为seven且密码为123时,显示登录成功,否则登录失败,失败时允许重复输入三次
    count=1
    while count <= 3:
        name = input('请输入用户名: ')
        psw = input('请输入密码: ')
        if name == 'seven' and psw == '123':
            print('登陆成功')
            break
        else:
            print('登录失败')
            count+=1
            continue
    #c实现用户输入用户名和密码,当用户名为seven或alex且密码为123时,显示登录成功,否则登录失败,失败时允许重复输入三次
    count=1
    while count <= 3:
        name = input('请输入用户名: ')
        psw = input('请输入密码: ')
        if name == 'seven' and psw == '123':
            print('登陆成功')
            break
        elif name == 'alex' and psw == '123':
            print('登陆成功')
            break
        else:
            print('登录失败')
            count+=1
            continue
    
    #习题2
    #a使用while循环实现输出2-3+4-5+6...+100的和
    res=0
    count=2
    while count <= 100:
        if count%2 == 1:
            res-=count
        else:
            res+=count
        count+=1
    print(res)
    #b使用for循环和range实现输出1-2+3-4+5-6...+99的和
    res=0
    for i in range(1,100):
        if i%2== 1:
            res+=i
        else:
            res-=i
        i+=1
    print(res)
    
    #c使用while循环实现输出1,2,3,4,5   7,8,9  11,12
    res=1
    while res <=12:
        if res == 6 or res ==10:
            res+=1
            continue
        print(res)
        res+=1
    #d使用while循环实现输出1-100内的所有奇数
    res=1
    while res <= 100:
        if res%2 == 1:
            print(res)
        else:
            res+=1
            continue
        res+=1
    #e使用while循环实现输出1-100内的所有偶数
    res=1
    while res <=100:
        if res%2 == 0:
            print(res)
        else:
            res+=1
            continue
        res+=1
    #练习三
    #a移除name变量对应的值的两边的空格,并输入移除有的内容
    # name='alex'
    # print(name.strip())
    #b判断name变量对应的值是否以"al"开头,并输出结果
    # name='alex'
    # print(name.startswith('al'))  #True
    #c判断name变量对应的值是否以"x"结尾,并输出结果
    # name='alex'
    # print(name.endswith('x'))       #True
    #d将name变量对应的值中的"l"替换为"p",并输出结果
    # name='alex'
    # print(name.replace('l','p'))
    # e.将name变量对应的值根据"l"分割,并输出结果
    # name='alex'
    # print(name.split('l'))
    # f.请问,上一题e分割之后得到的值是什么类型
    # name='alex'
    # print(type(name.split('l')))
    # g.将name变量对应的值变大写,并输出结果
    # name='alex'
    # print(name.upper())
    # h.将name变量对应的值变大小写,并输出结果
    # name='alex,ALex'
    # print(name.upper())
    # print(name.lower())
    # i.请输出name变量对应的值的第2个字符?
    # name='alex'
    # print(name[1])
    # j.请输出name变量对应的值的前3个字符?
    # name='alex'
    # print(name[0:3])
    # k.请输出name变量对应的值的后2个字符?
    # name='alex'
    # print(name[-2:])
    # l.请输出name变量对应的值中"e"所在的索引位置?
    # name='alex'
    # print(name.find('e'))
    
    
    #练习四:写代码,有如下列表,按照要求实现每一个功能
    # li = ['alex','eric','rain']
    # a.计算列表长度并输出
    # print(len(li))
    
    # b.列表中追加元素"seven",并输出添加后的列表
    li.append('seven')
    print(li)
    
    # c.请在列表的第1个位置插入元素"Tony",并输出添加后的列表
    li.insert(0,'Tony')
    print(li)
    
    # d.请修改列表第2个位置的元素为"Kelly",并输出修改后的列表
    li[1]='kelly'
    print(li)
    
    # e.请删除列表中的元素"eric",并输出修改后的列表
    li.remove('eric')
    print(li)
    
    
    # f.请删除列表中的第2个元素,并输出删除元素的值和删除元素后的列表
    print(li.pop(1))
    print(li)
    
    # g.请删除列表中的第3个元素,并输出删除元素后的列表
    li.pop(2)
    print(li)
    
    # h.请删除列表中的第2至4个元素,并输出删除元素后的列表
    li = ['alex','eric','rain','lina']
    del li[1:5]
    print(li)
    
    # i.请将列表所有的元素反转,并输出反转后的列表
    li.reverse()
    print(li)
    
    #*竖三角形
    l='*'
    for i in range(1,10):
        print(i*l)
    
    #*倒竖三角
    #方法1
    l='*'
    i=9
    while i > 0:
        print(i*l)
        i-=1
    #方法2
    for i in range(1,12):
        for j in range(1,12-i):
            print('*',end='')
        print()
    #取出列表中的名字,年龄,出生的年,月,日
    data=['alex',49,[1900,3,18]]
    print(data[0],data[1],data[2][0],data[2][1],data[2][2])
    
    #去掉重复
    names=['egon','alex','egon','wupeiqi']
    print(list(set(names)))
    
    #去掉重复,且保证列表顺序与原来保持一致
    names=['egon','alex','egon','wupeiqi']
    res=[]
    for i in names:
        if i not in res:
            res.append(i)
        print(res)
    
    
    #去掉重复,且保证列表顺序与原来保持一致
    names=[[1,2],3,[1,2],4]
    res=[]
    for i in names:
        if i not in res:
            res.append(i)
            print(res)
    
    
    #统计s='hello alex alex say hello sb sb'中每个单词的个数
    s='hello alex alex say hello sb sb'
    l=(list(s.split(' ')))
    s1=set(l)
    for i in s1:
        print(i,l.count(i))
    
    #练习一:
    # if True or False and False:
    #     print('yes')
    # else:
    #     print('no')
    #输出结果为?为什么?
    # 输出结果为yes,因为 False and False = False,True or False = True
    
    # if (True or False) and False:
    #     print('yes')
    # else:
    #     print('no')
    #输出结果为?为什么?
    # 输出结果为no,因为 True or False = True , True and False = False
    
    #练习二:编写if多分支,猜老男孩的年纪
    oldbpy_age=67
    while True:
        age=input('请输入老男孩的年纪: ')
        age=int(age)
        if age == oldbpy_age:
            print('恭喜你猜对了')
            break
        elif age < oldbpy_age:
            print('猜小了,傻逼')
            continue
        else:
            print('猜大了傻逼')
    #练习三:用户输入用户名密码验证,验证通过后进入子循环,输入命令,命令若为q,则退出所有循环
    tag=True
    while tag:
        name = input('请输入用户名: ')
        psw = input('请输入密码: ')
        if name == 'sam' and psw == '123':
            print('登陆成功')
        else:
            print('请重新输入: ')
            continue
        while tag:
            cmd = input('请输入命令(q退出): ')
            if cmd =='q':
                tag = False
            else:
                print('输入的命令为: ',cmd)
                continue
    
    #练习四:循环取出元组中所有元素:方式一:while和for(按照索引),方式二:不按照索引的方式
    t=(1,2,3,4,5,6,7,8,9)
    s=0
    while s < len(t):
        print(t[s])
        s+=1
    
    t=(1,2,3,4,5,6,7,8,9)
    for i in t:
        print(t[i-1])
    
    # # 方式二、
    for i in t:
        print(i)
    
    #练习五:循环读取列表以及子列表中所有元素
    l=[1,2,[3,4],[5,6]]
    for i in l:
       # print(i)
        if type(i) == list:
            for j in i:
                print(j)
        else:
            print(i)
    
    #练习六:打印
    '''
       *
      ***
     *****
    *******
    '''
    l='*'
    for j in range(1,8,2):
        l1 = j*l
        print(l1.center(7,' '))
    
    #练习七:打印
    '''
     *****
      ***
       *
    '''
    l='*'
    l1=6
    for i in range(1,6,2):
        l2=(6-i)*l
        print(l2.center(5,' '))
    
    
    #练习八:打印
    '''
    *
    **
    ***
    ****
    *****
    '''
    l='*'
    for i in range(1,6):
        l1=i*l
        print(l1.ljust(5,' '))
    #练习九:打印
    '''
    ******
    *****
    ****
    ***
    **
    *
    '''
    for i in range(1,7):
        for j in range(1,8-i):
            print('*',end='')
        print()
    #练习十:编写登陆接口
    # 基础需求:
    # 让用户输入用户名密码
    # 认证成功后显示欢迎信息
    # 输错三次后退出程序
    res=1
    while res<=3:
        name=input('请输入用户名: ')
        psw = input('请输入密码: ')
        if name == 'sam' and psw == '123':
            print('登陆成功')
            break
        else:
            print('登陆失败,请重新登录')
            res+=1
            continue
    #数据类型练习题:
    练习一:有十进制数n=10
    转成二进制
    转成八进制
    转成十六进制
    n=10
    print(bin(n))   #二进制
    print(oct(n))   #八进制
    print(hex(n))   #十六进制
    
    #练习二:与用户交互,要求用户输入年龄和薪资,将用户输入的年龄转成整形,将用户输入的薪资转成浮点型
    res =1
    while res <2 :
        nl=input('请输入年龄: ')
        nl=int(nl)
        xz=input('请输入薪资: ')
        xz=float(xz)
        print(type(nl))
        print(type(xz))
        res+=1
    
    #练习三:
    '''
    用户输入用户名,年纪,工作,爱好,格式化输出如下内容(使用%s和format两种方式)
    ------------ info of Alex Li -----------
    Name  : Alex Li
    Age   : 22
    job   : Teacher
    Hobbie: girl
    ------------- end -----------------
    '''
    # 方式一
    l='Name:{},Age:{},Job:{},Hobbie:{}'
    for i in range(1,2):
        name=input('请输入用户名: ')
        age=input('请输入年纪: ')
        job=input('请输入工作: ')
        hobbie=input('请输入爱好: ')
        # l=l.format(name,age,job,hobbie)
        print(name.center(30,'-'))
        print('Name: %s
    Age: %s
    Job: %s
    Hobbie: %s'%(name,age,job,hobbie))
        e='end'
        print(e.center(30,'-'))
    #方式二
    l='Name:{z},Age:{x},Job:{c},Hobbie:{v}'
    e='end'
    name=input('请输入用户名: ')
    age=input('请输入年纪: ')
    job=input('请输入工作: ')
    hobbie=input('请输入爱好: ')
    l=l.format(z=name,x=age,c=job,v=hobbie)
    l1=list(l[0:].split(','))
    print(name.center(30,'-'))
    for i in l1:
        print(i)
    print(e.center(30,'-'))
    #练习四:
    s='alex say hello'
    切片取出第say
    print(s[5:8])
    切片取出倒数后两个字符
    print(s[-2:])
    
    
    #练习五:
    # 编写循环,让用户输入年纪,如果输入为空,或者不为数字,则重新输入
    while True:
        age = input('请输入年纪: ')
        if age.isspace() == True or age.isdigit() == False:
            print('请重新输入')
            continue
        else:
            print('您的年纪是:',age)
            break
    
    #练习六:
    # 用列表模拟上电梯的流程(队列)
    #     循环生成一个1000个值的列表(入队)
    #     循环取走这个1000个值(出队)
    l=[]
    for i in range(1,1001):
        l.append(i)
        print(l)
    for j in range(1,1001):
        print(l.pop(0))
    
    # 用列表模拟把衣服放箱子里,然后取衣服的流程(堆栈)
    #     循环生成一个1000个值的列表(入栈)
    #     循环取走这个1000个值(出栈)
    l=[]
    for i in range(1,1001):
        l.append(i)
        print(l)
    for j in range(1,1001):
        print(l.pop())
    #练习七:
    dicta={'a':1,'b':2,'c':3,'d':'hello'}
    dictb={'b':3,'c':2,'d':'world','f':10}
    两字典相加,不同的key对应的值保留,相同的key对应的值相加后保留,如果是字符串就拼接(字符串拼接'hello'+'world''helloworld')
    {'a': 1, 'b': 5, 'c': 5, 'd': 'helloworld', 'f': 10}
    dictc=dicta
    for i in dictb:
        if i in dictc:
            dictc[i]=dictb[i]+dictc[i]
        else:
            dictc[i]=dictb[i]
        print(dictc)
  • 相关阅读:
    mvn clean deploy
    数据库分库分表,读写分离
    耳鸣治疗法
    Navicat Preminum
    spring boot 获取bean
    java中集合Collection转list对象
    Java8新特性之Collectors
    spring 给一个类 生成test
    Spring注解标签详解@Autowired @Qualifier等 @Slf4j
    linux定时执行脚本
  • 原文地址:https://www.cnblogs.com/78pikaqiu/p/6985013.html
Copyright © 2011-2022 走看看