Board logo

標題: for 迴圈 (三) [打印本頁]

作者: 鄭繼威    時間: 2023-1-30 14:04     標題: for 迴圈 (三)

本帖最後由 鄭繼威 於 2023-1-31 10:58 編輯

#range(start,stop,step)
#預設start=0,stop,step=1
#range(1,6)->[1,2,3,4,5]

試宣告兩個串列,一個存放班級名稱,一個存放人數,搭配 for 迴圈顯示五年級各班人數,執行畫面如下:

  1. #班級
  2. list1=["忠","孝","仁","愛","信","義","和","平"]
  3. #人數
  4. list2=[28,27,27,26,28,29,30,27]

  5. for i in range(8):
  6.     print("五年{}班有{}人".format(list1[i],list2[i]))
複製代碼

作者: 李允呈    時間: 2023-1-31 11:26

  1. list1=["忠","孝","仁","愛","信","義","和","平"]
  2. list2=[1,2,3,4,5,6,7,8]
  3. for i in range(8):
  4.     print(f"五年{list1[i]}有{list2[i]}人")
複製代碼

作者: 李宗祐    時間: 2023-1-31 11:27

  1. list=["忠","孝","仁","愛","信","義","和","平"]
  2. list2=[28,27,27,26,28,29,30,27]
  3. for i in range(8):
  4.     print("五年{}班有{}人".format(list[i],list2[i]))
複製代碼

作者: 莊硯羽    時間: 2023-1-31 11:27

  1. list1=["忠","孝","仁","愛","信","義","和","平"]
  2. list2=[28,27,27,26,28,29,30,27]
  3. for i in range(8):
  4.     print("五年{}班有{}人".format(list1[i],list2[i]))
複製代碼

作者: 劉奕劭    時間: 2023-1-31 11:30

  1. list1=["忠","孝","仁","愛","信","義","和","平"]
  2. list2=[27,30,28,30,29,37,30,31]
  3. for i in range(8):
  4.     print(f"五年{list1[i]}班有{list2[i]}人")
複製代碼





歡迎光臨 種子論壇 | 高雄市資訊培育協會學員討論區 (http://seed.istak.org.tw/) Powered by Discuz! 7.2