day5 作业

1.求s=a+aa+aaa+aaaa的值,例2+22+222+2222

size = int(input('enter the list\'size:'))  //确定列表的长度
number1 = int(input('enter the number1:')) //确定列表元素的基数
list1 = []   //创建空列表1,用于存放给定的长度加基数的元素
list2 = []  //空列表2,用于实现列表元素相加的字符串输出
for x in range(1,size+1):
    list1.append(str(number1)*x)
print(list1)  //向列表1添加给定的大小与基数,字符串添加
str1 =''
sum1 = 0 //定义和的初值
for y in list1: 
    sum1+=int(y) //求列表元素的和,先转换为int型
    list2.append(y)//向列表2添加列表1的元素
    list2.append('+')//添加运算符
list2.pop()//去掉最后位的+号
for z in list2:
    str1+=str(z)//以字符单个取出列表2元素相加

print('%s = %d'%(str1,sum1))


enter the list'size:5
enter the number1:2
['2', '22', '222', '2222', '22222']
2+22+222+2222+22222 = 24690

2.打印直角三角形

number1 = int(input('enter the number1:'))
for x in range(1,number1+1):
    for y in range(1,x+1):
        print('*',end='')
    print()
    
enter the number1:5
*
**
***
****
*****

3.打印等腰三角形

number1 = int(input('enter the number1:'))
for x in range(1,number1+1):
    for y in range(number1,x,-1):
        print(' ',end='')
    for z in range(1,2*x):
        print('*',end='')
    print()

enter the number1:5
    *
   ***
  *****
 *******
*********

4.打印倒直角三角形

number1 = int(input('enter the number1:'))
for x in range(1,number1+1):
    for y in range(number1,x-1,-1):
        print('*',end='')
    print()


enter the number1:5
*****
****
***
**
*

5.输入两个正整数m,n求最大公约数和最小公倍数



6.一个数若刚好等于它的因子之和,如6=1+2+3,则它是完美数,求1000以内所有的完美数。



7.输出九九乘法表

for x in range(1,10):
    for y in range(1,x+1):
        print('%d*%d=%d'%(x,y,x*y),end=' ',sep='\t')
    print()

1*1=1 
2*1=2 2*2=4 
3*1=3 3*2=6 3*3=9 
4*1=4 4*2=8 4*3=12 4*4=16 
5*1=5 5*2=10 5*3=15 5*4=20 5*5=25 
6*1=6 6*2=12 6*3=18 6*4=24 6*5=30 6*6=36 
7*1=7 7*2=14 7*3=21 7*4=28 7*5=35 7*6=42 7*7=49 
8*1=8 8*2=16 8*3=24 8*4=32 8*5=40 8*6=48 8*7=56 8*8=64 
9*1=9 9*2=18 9*3=27 9*4=36 9*5=45 9*6=54 9*7=63 9*8=72 9*9=81 

8.一个五位数判断它是不是回文数字,12321


number = int(input('enter the number:'))
str1 = str(number)
str2 = str1[::-1]
if number<10000 or number>99999:
    print('input error!')
elif number == int(str2):
    print('yes!')
else:
    print('no')

enter the number:12321
yes!

9.打印菱形

for x in range(1,5):
    for y in range(5,x+1,-1):
        print(' ',end='')
    for z in range(1,2*x):
        print('*',end='')
    print()
for x in range(1,4):
    for a in range(1,x+1):
        print(' ',end='')
    for b in range(1,2*(4-x)):
        print('*',end='')
    print()

   *
  ***
 *****
*******
 *****
  ***
   *
[Finished in 0.2s]

10.输入一个字符串,判断有几个空格,几个数字,几个英文字母,几个其他字符。


str1 = input('enter the str:')
index1 = str1.count(' ',0,len(str1))
count1 = 0
count2 = 0
count3 = 0
for x in range(0,len(str1)):
    str2 = str1[x]
    if str2.isdigit()==True:
        count1+=1
    elif str2.isalpha()==True:
        count2+=1
    else:
        count3 = len(str1) - index1 - count1 - count2
print('该字符串有%d个数字,%d个字母,%d个其他字符,%d个空格'%(count1,count2,count3,index1))

enter the str:1 2 abd__*&
该字符串有2个数字,3个字母,4个其他字符,2个空格

11.已知一个列表,求中心元素


n = int(input('enter the number\'s size:'))
numbers = []
for x in range(0,n):
    data = input('enter the numbers\'s data:')
    numbers.append(data)
print(numbers)
if len(numbers)%2!=0:
    index1 = len(numbers)//2
    print('中心元素:%s'%numbers[index1])
else:
    index2 = len(numbers)//2
    print('中心元素:%s,%s'%(numbers[index2-1],numbers[index2]))

enter the number's size:5
enter the numbers's data:1
enter the numbers's data:2
enter the numbers's data:3
enter the numbers's data:4
enter the numbers's data:5
['1', '2', '3', '4', '5']
中心元素:3

12.已知一个列表求所有元素和

n = int(input('input the size:'))
numbers = []
sum1 = 0
for x in range(0,n):
    str1 = input('input the data:')
    numbers.append(str1)
    sum1+=int(numbers[x])

print(numbers)
print(sum1)

input the size:5
input the data:2
input the data:3
input the data:4
input the data:5
input the data:6
['2', '3', '4', '5', '6']
20

13.已知一个列表,输出奇数下标元素

numbers = [1,2,3,4,5,6,7,8]
print('偶数',numbers[0:len(numbers):2])
print('奇数',numbers[1:len(numbers):2])

偶数 [1, 3, 5, 7]
奇数 [2, 4, 6, 8]
[Finished in 0.2s]

14.已知一个列表输出列表中值为奇数的

numbers = [2,434,67,24,658,32,37,347]
for x in numbers:
    if int(x%)2!=0:
        print(x)

67
37
347
[Finished in 0.3s]

15.已知列表,所有元素*2

numbers = [2,435,5,7,8,9,322]
numbers1 = []
for x in numbers:
    numbers1.append(int(x)*2)
print(numbers1)
    
[4, 870, 10, 14, 16, 18, 644]
[Finished in 0.2s]

16.已知一个列表将所有元素加到第一个元素的位置

17.已知一个列表,将奇数位元素与偶数位元素分开保存到2个新列表中。

number1 = [1,2,3,4,5,6]
number2 = []
number3 = []
for x in range(0,len(number1)):
    if x%2!=0:
        
        number2.append(number1[x])
    elif x%2==0:
        
        number3.append(number1[x])
print('奇数',number2)
print('偶数',number3)

奇数 [2, 4, 6]
偶数 [1, 3, 5]
[Finished in 1.7s]

18.把A列表前5个元复制到B列表中

numbers = [1,2,3,4,5,6,7]
number1 = []
number2 = []
for x in range(0,5):
    number1.append(numbers[x])
number2 = numbers[0:5].copy()
print(number2)
print(number1)

[1, 2, 3, 4, 5]
[1, 2, 3, 4, 5]
[Finished in 1.5s]

19.把1-36分别放入列表中,计算列表对角线元素之和 6*6列表。



20.列表排序

numbers = []
for _ in range(10):
    number = int(input('input number:'))
    numbers.append(number)
numbers.sort()
print(numbers)

input number:1
input number:3
input number:2
input number:5
input number:4
input number:6
input number:9
input number:8
input number:10
input number:7
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

21.删除列表中某个元素

numbers = [1,2,3,4,5,6,7,8,9,0]
numbers.pop(2)
print(numbers)

[1, 2, 4, 5, 6, 7, 8, 9, 0]
[Finished in 0.4s]

22.列表长度为10 ,递增排列,插入一个数,按递增排。

numbers = [0,2,4,6,8,10,12,14,16,18]
numbers.append(int(3))
numbers.sort()
print(numbers)

[0, 2, 3, 4, 6, 8, 10, 12, 14, 16, 18]
[Finished in 0.2s]


numbers = [1,22,3,52,4,5,445,63,48,60]
for x in range(0,len(numbers)):
    for y in range(0,len(numbers)-x-1):
        if numbers[y]

23.列表为10,去重

numbers = [1,2,3,3,4,2,3,2,5,6,1]
new_ids = []
for id in numbers:
    if id not in new_ids:
        new_ids.append(id)
print(new_ids)

[1, 2, 3, 4, 5, 6]

24.删除列表中下标3-6的元素

numbers = [1,2,3,3,4,2,3,2,5,6,1]
del numbers[3:6]
print(numbers)

[1, 2, 3, 3, 2, 5, 6, 1]

25.已知列表a,b 定义c,c包含a,b 去重

a = [1,2,4,6,7]
b = [2,3,4,5,6,9]
c = []
for x in range(0,len(a)):
    if a[x] not in c:
        c.append(a[x])
for y in range(0,len(b)):
    if b[y] not  in c:
        c.append(b[y])
print(c)

[1, 2, 4, 6, 7, 3, 5, 9]

学生管理系统

'''
系统对应的大的容器是字典--->字典中的某一个key对应的值是所有的学生
每个学生是一个字典{'student':[{学生1},{学生2},{学生3}]}
'''
# system_info={'student':[{key_name:value,key_id:value,key_tel:value,key_age:value},{},{}]}
key_all_student = 'students'
system_info = {}
key_name = 'name'
key_age = 'age'
key_tel = 'tel'
key_id = 'id'
# number表示有几个学生用来随机生成学生学号
number = 0
while True:
    print('='*30)
    print('欢迎进入学生管理系统')
    print('1.添加学生\n2.查看学生信息\n3.删除学生\n4.修改学生信息\n0.退出')
    print('='*30)
    choose = input('选择操作!:')
    if choose=='0':
        break
    if choose=='1':
        while True:
            print('==添加学生==')

            add_name = input('姓名')
            add_age = input('年龄')
            add_tel = input('电话')
            # 产生学号
            number+=1
            add_id = 'cd1805'+str(number).rjust(3,'0')
            # 创建学生(一个学生就是一个字典)
            add_student = {key_name:add_name,key_age:add_age,key_tel:add_tel,key_id:add_id}
            # 获取存学生的容器
            all_student=system_info.get(key_all_student)
            # 判断system_info容器是否为空,为二次添加判断
            if  all_student == None:
                all_student = []
            all_student.append(add_student)
            system_info[key_all_student]=all_student
            print('添加学生%s成功'%add_name)
        #   给出选择
            print(system_info)
            print('1.继续添加')
            print('2.返回上一级')
            choose1 = int(input('选择操作'))
            if choose1!=1 or choose1==2:
                break

        continue
    if choose=='2':
        while True:
            print('查看学生\n1.按姓名查找\n2.查找所有信息\n3.根据学号查看\n4.返回上一层')
            choose2 = input('选择操作')
            flag =False
            # flag表示是否找不到学生
            if choose2=='1':
                name = input('请输入姓名')
                for student in all_student:
                    if name==student[key_name]:
                        print(student)
                        flag = True
                if flag==False:
                    print('没找到学生信息!')


            # 如果学生名字和输入名字相同,打印学生信息

            if choose2=='2':
                # print(system_info)
                all_student=system_info.get(key_all_student)
                # 系统还没有添加学生
                # or和and的短路操作:
                # 表达式1 or表达式2:如果表达式1位True,那么不会执行2,结果为True
                # 表达式1 and 表达式2:表达式1值为False,不会执行表达式2,结果为False
                if (not all_student) or (not len(all_student)):
                    print('系统中没有学生')
                    break
                #有学生时
                for student in all_student:
                    print('姓名:%s,年龄:%s,学号:%s,电话:%s'%\
                          (student[key_name],student[key_age],student[key_id],student[key_tel]))

            if choose2=='3':
                idnum = input('请输入学号')
                flag = False
                for student in all_student:
                    if idnum==student[key_id]:
                        print(student)
                        flag=True
                if flag==False:
                    print('没有找到学生信息')
            if choose2=='4':
                print('返回上一层')
                break
    if choose=='3':
        while True:
            print('删除学生\n1.根据姓名删除\n2.根据学号删除\n3.返回上级')
            choose3 = input('选择操作')
            all_student = system_info.get(key_all_student)
            if (not all_student) or (not len(all_student)):
                print('该系统没有学生')
                break
            if choose3=='1':
                name = input('输入要删除的学生姓名')
                flag = False
                list1 = []
                for student in all_student:
                    if name==student[key_name]:
                        list1.append(student)

            #   学生可能存在重名,所以需要把满足条件的姓名取出来,单独存放,再继续判断
                if not len(list1):
                    print('没有找到相关学生')
                    continue
                # 打印所有需要删除的学生
                index1 = 0
                for student in list1:
                    print(index1,student)
                    index1+=1
                index2 = int(input('请选择要删除的学生的标号0-%d'%\
                               (len(list1)-1)))
                del_student = list1[index2]
            #   从系统中删除学生
                system_info[key_all_student].remove(del_student)


            if choose3=='2':
                idnum = input('输入你要删除的学生学号')
                all_student = system_info.get(key_all_student)
                if (not all_student) or (not len(all_student)):
                    print('该系统没有学生')
                    break
                for x in all_student[:]:
                    if idnum==x[key_id]:
                        system_info[key_all_student].remove(x)
                print(system_info)

            if choose3=='3':
                break
    if  choose=='4':
        while True:
            print('修改学生信息\n1.按姓名修改\n2.按学号修改\n3.返回上一级')
            all_student = system_info.get(key_all_student)
            if (not all_student) or (not len(all_student)):
                print('该系统没有学生')
                break
            choose4 = input('选择操作:')
            if choose4=='1':
                name = input('输入你要修改的学生姓名:')
                flag = False
                list1 = []
                for student in all_student:
                    if name == student[key_name]:
                        list1.append(student)
                index1 = 0
                for student in list1:
                    print(index1, student)
                    index1 += 1
                index2 = int(input('请选择要修改的学生的标号0-%d' % \
                                   (len(list1) - 1)))
                update_student = list1[index2]
                suoyin1 = system_info[key_all_student].index(update_student)
                while True:
                    print('选择你要修改哪些学生信息:\n1.姓名\n2.年龄\n3.电话\n4.学号\n5.返回上级')
                    choose5 = input('选择操作')
                    if choose5=='1':
                        system_info[key_all_student][suoyin1][key_name]=input('修改的姓名:')

                    if choose5=='2':
                        system_info[key_all_student][suoyin1][key_age] = input('修改的年龄:')
                    if choose5=='3':
                        system_info[key_all_student][suoyin1][key_tel] = input('修改的电话:')
                    if choose5=='4':
                        system_info[key_all_student][suoyin1][key_id] = input('修改的学号:')
                    if choose5=='5':
                        break
            if choose4=='2':
                numid = input('输入你要修改的学生学号:')
                for x in all_student:
                    if numid==x[key_id]:
                        suoyin2 = system_info[key_all_student].index(x)

                while True:
                    print('选择你要修改哪些学生信息:\n1.姓名\n2.年龄\n3.电话\n4.学号\n5.返回上级')
                    choose5 = input('选择操作')
                    if choose5 == '1':
                        system_info[key_all_student][suoyin2][key_name] = input('修改的姓名:')

                    if choose5 == '2':
                        system_info[key_all_student][suoyin2][key_age] = input('修改的年龄:')
                    if choose5 == '3':
                        system_info[key_all_student][suoyin2][key_tel] = input('修改的电话:')
                    if choose5 == '4':
                        system_info[key_all_student][suoyin2][key_id] = input('修改的学号:')
                    if choose5 == '5':
                        break

            if choose4=='3':
                break

你可能感兴趣的:(day5 作业)