某大学作业

1.1

#1.1
import random
list1=[]
for i in range(10):
    list1.append(random.randint(10,100))
def sort_list(list):
    n=len(list)
    for i in range(n-1):
        min_index =i;  # 最大只能到99
        for j in range(i+1,n):
            if(list[min_index]>list[j]):
                min_index=j
        #找到每一轮最小的
        if(min_index!=i):
            list[i],list[min_index]=list[min_index],list[i]
    return  list;
print(sort_list(list1))

1.2

list1=[2,3,5,7,11.13,17,19,23]
n=int(input())
list2=[]
i=0
while(n!=1):
    if(n%list1[i]!=0):
        i+=1
    n=n/list1[i]
    list2.append(list1[i])
#格式化输出
s="60="
for i in range(len(list2)):
    if(i==(len(list2)-1)):
        s+=str(list2[i])
        break
    s+=str(list2[i])+"X"
print(s)

1.3

import  random
dict1={"一等奖命中次数":0,
       "二等奖命中次数":0,
       "三等奖命中次数":0}
for i in range(1000):
    k=random.randint(0,100)
    if(k in range(0,8)):
        dict1["一等奖命中次数"]=dict1["一等奖命中次数"]+1;
        continue
    if (k in range(9, 30)):
        dict1["二等奖命中次数"] = dict1["二等奖命中次数"] + 1;
        continue
    if (k in range(31, 100)):
        dict1["三等奖命中次数"] = dict1["三等奖命中次数"] + 1;
        continue
print(dict1)

1.4

n=str(input())
if(n.count('.')!=3 ):
    print("IP地址不合法")
str1=""
ba=0
for i in n:
    if(i=='.'):
        str1=""
        continue
    str1 += i
    if(int(str1) not in range(0,256)):
        print("IP地址不合法")
        ba=1
if(ba==0): print("IP地址合法")


2.1

class Student:
    def __init__(self,name,age,scores):
        self.name=name
        self.age=age
        self.scores=scores
    def get_age(self):
        return  self.age
    def get_name(self):
        return  self.name
    def get_scores(self):
        return  max(self.scores)


2.2

class HSStudent(Student):
    def __init__(self, name, age, scores,fivescores):
        super().__init__(name, age, scores)
        self.fivescores=fivescores
    def  get_aver(self):
        return  (sum(self.scores)+sum(self.fivescores))//(len(self.scores)+len(self.fivescores))
    def get_max_score(self):
        return  max(max(self.scores),max(self.fivescores))

h=HSStudent("name",12,[10,9,8],[45,45,78])
print(h.get_max_score())
print(h.get_aver())

2.3

class IntException(RuntimeError):
    def __init__(self,err='输入的数必须为整数'):
        Exception.__init__(self,err)

class ChuException(RuntimeError):
    def __init__(self,err='除数不能为0'):
        Exception.__init__(self,err)

a=int(input())
b=int(input())
try:
    if(isinstance(a,int) and isinstance(b,int)):
        if (b == 0):
            raise ChuException()
        print(a // b)
    else:
        raise IntException()
except IntException as e:
    print(e)
except ChuException as e:
    print(e)

上一篇:3、SystemInit时钟配置分析


下一篇:【Python】第三章(容器)综合练习