Python3.8.2 <<
Previous Next >> Assignment 2
Random grouping
report
亂數分組程式流程:
1.抓出學員資料
利用Python的requests模組來擷取網頁上的內容
2.將抓出的資料轉為文字檔
設定一變數將剛剛擷取出來的內容轉為文字檔方便之後做處理
3.將文字檔內容轉為數列
利用split語法將剛剛的文字內容以空格作為分割依據來建立一數列
4.使用random模組將學員資料打亂
將上述的數列內容由原本依照學號排序之順序改為亂數排序
5.利用迴圈及判斷式列印文字
利用第一層迴圈來顯示1~4組,接著利用判斷式來分辨目前分到第幾組了並再次利用迴圈來做組員分配
import requests
import random
url = "https://mde.tw/cd2020/downloads/2020spring_cd_2a_list.txt"
Msource = requests.get(url)
#print(Msource.text)
member = Msource.text
mlist = member.split("\n")
mlist.pop()
#print(member)
mlen = len(mlist)
#print(mlist[0:13])
random.shuffle(mlist)
g1 = 13
g2 = 2*g1
g3 = 3*g1
g4 = int(mlen)
print("全班共",mlen,"人")
for i in range(1,5):
print("Group",i,":")
if (i == 1):
for j in range(0,g1):
print(mlist[j])
j += 1
print("-"*20)
elif (i == 2):
for m in range(g1,g2):
print(mlist[g1])
g1 += 1
print("-"*20)
elif (i == 3):
for k in range(g2,g3):
print(mlist[g2])
g2 += 1
print("-"*20)
elif (i == 4):
for l in range(g3,g4):
print(mlist[g3])
g3 += 1
print("-"*20)
print(mlist)
===================
Python3.8.2 <<
Previous Next >> Assignment 2