運行結果:
源代碼:
"""
項目:類似于學生管理系統---增刪改查
"""
#封裝一個學生類
import random
class Student:
? ? def __init__(self,stuid,name,score):
? ? ? ? self.stuid = stuid
? ? ? ? self.name = name
? ? ? ? self.score = score
? ? def show_info(self):
? ? ? ? print(f"{self.stuid},{self.name},{self.score}")
stu_list=[]
#生成學號,4位數隨機的,唯一的
def get_stuid():
? ? while True:
? ? ? ? #產生一個隨機學號
? ? ? ? stu_id = random.randint(1000,9999)
? ? ? ? #遍歷學生list,查看學號是否被占用
? ? ? ? for stu in stu_list:
? ? ? ? ? ? if stu.stuid == stu_id:#占用
? ? ? ? ? ? ? ? break;
? ? ? ? else:
? ? ? ? ? ? return stu_id