Board logo

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

作者: 鄭繼威    時間: 2022-8-18 22:26     標題: for 迴圈 (三)

本帖最後由 鄭繼威 於 2022-8-19 14:57 編輯

#range(start,stop,step)
#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]))
複製代碼

作者: 鄭繼威    時間: 2022-8-19 14:55

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

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

作者: 彭郁程    時間: 2022-8-19 14:55

本帖最後由 彭郁程 於 2022-8-19 14:59 編輯
  1. # -*- coding: utf-8 -*-
  2. """
  3. Created on Fri Aug 19 14:24:25 2022

  4. @author: student
  5. """

  6. list1=["甲","乙","丙","丁","戊","己","庚","辛"]
  7. list2=[28,27,27,26,28,29,30,27]
  8. for i in range(8):
  9.     print("五年{}班有{}人".format(list1[i],list2[i]))
複製代碼
  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]))
複製代碼

作者: 陳牧謙    時間: 2022-8-19 14:59

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

作者: 簡宗漢    時間: 2022-8-19 14:59

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

作者: 彭郁程    時間: 2022-8-19 14:59

本帖最後由 彭郁程 於 2022-8-19 15:04 編輯
  1. # -*- coding: utf-8 -*-
  2. """
  3. Created on Fri Aug 19 14:24:25 2022

  4. @author: student
  5. """

  6. list1=["甲","乙","丙","丁","戊","己","庚","辛"]
  7. list2=[28,27,27,26,28,29,30,27]
  8. for i in range(8):
  9.     print("五年{}班有{}人".format(list1[i],list2[i]))
複製代碼

作者: 吳俊頡    時間: 2022-8-19 15:00

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

作者: 姜宏叡    時間: 2022-8-19 15:02

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

作者: 吳鎧茵    時間: 2022-8-19 15:02

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

作者: 胡凰月    時間: 2022-8-19 15:04

  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]))
複製代碼





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