Notice
Recent Posts
Recent Comments
Link
«   2025/05   »
1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31
Archives
Today
Total
관리 메뉴

RUBY

리스트와 while문(파이썬) 본문

프로그래밍 언어/Python

리스트와 while문(파이썬)

ruby-jieun 2023. 1. 22. 21:41

 

 

리스트와 while문

 


 

 

 

리스트와 while문

  • while문을 이용하면 다양한 방법으로 아이템 조회가 가능하다.
cars = ['그랜저', '소나타', '말리부', '카니발', '쏘렌토']
n = 0
while n < len(cars):
    print(cars[n])
    n += 1
cars = ['그랜저', '소나타', '말리부', '카니발', '쏘렌토']
n = 0
flag = True
while flag:
    print(cars[n])
    n += 1
    if n == len(cars):
        flag = False
cars = ['그랜저', '소나타', '말리부', '카니발', '쏘렌토']
n = 0
while True:
    print(cars[n])
    n += 1
    if n == len(cars):
        break
그랜저
소나타
말리부
카니발
쏘렌토

studentCnts = [[1, 19], [2, 20], [3, 22], [4, 18], [5, 21]]
n = 0
while n < len(studentCnts):
    print('{}학급 학생수: {}'.format(studentCnts[n][0], studentCnts[n][1]))
    n += 1
1학급 학생수: 19
2학급 학생수: 20
3학급 학생수: 22
4학급 학생수: 18
5학급 학생수: 21

 

아래 표와 리스트를 이용해서 학급별 학생 수와 전체 학생 수 그리고 평균 학생수를 출력해보자

studentCnts = [[1, 18], [2, 19], [3, 23], [4, 21], [5, 20], [6, 22], [7, 17]]
sum = 0
avg = 0
n = 0
while n < len(studentCnts):
    classNo = studentCnts[n][0]
    cnt = studentCnts[n][1]
    print('{}학급 학생수: {}명'.format(classNo, cnt))
    sum += cnt
    n += 1
print('전체 학생 수: {}명'.format(sum))
print('평균 학생 수: {}명'.format(sum / len(studentCnts)))
1학급 학생수: 18명
2학급 학생수: 19명
3학급 학생수: 23명
4학급 학생수: 21명
5학급 학생수: 20명
6학급 학생수: 22명
7학급 학생수: 17명
전체 학생 수: 140명
평균 학생 수: 20.0명

 

  • while문과 if문을 이용해서 과락 과목 출력하기
minScore = 60
scores = [
    ['국어', 58],
    ['영어', 77],
    ['수학', 89],
    ['과학', 99],
    ['국사', 50]]
n = 0
while n < len(scores):
    if scores[n][1] < minScore:
        print('과락 과목: {}, 점수: {}'.format(scores[n][0], scores[n][1]))
    n += 1
과락 과목: 국어, 점수: 58
과락 과목: 국사, 점수: 50

minScore = 60
scores = [
    ['국어', 58],
    ['영어', 77],
    ['수학', 89],
    ['과학', 99],
    ['국사', 50]]
while n < len(scores):
    if scores[n][1] >= minScore:
        n += 1
        continue
    print('과락 과목: {}, 점수: {}'.format(scores[n][0], scores[n][1]))
    n += 1
과락 과목: 국어, 점수: 58
과락 과목: 국사, 점수: 50

 

 

while문을 이용해서 사용자가 국어, 영어, 수학, 과학, 국사 점수를 입력하면 과락 과목와 점수를 출력하는 프로그램을 만들어보자.

minScore = 60
korScore = int(input('국어 점수: '))
engScore = int(input('영어 점수: '))
matScore = int(input('수학 점수: '))
sciScore = int(input('과학 점수: '))
hisScore = int(input('국사 점수: '))
scores = [
    ['국어', korScore],
    ['영어', engScore],
    ['수학', matScore],
    ['과학', sciScore],
    ['국사', hisScore]]
n = 0
while n < len(scores):
    if scores[n][1] < minScore:
        print('과락 과목: {}, 점수: {}'.format(scores[n][0], scores[n][1]))
    n += 1
국어 점수: 80
영어 점수: 59
수학 점수: 66
과학 점수: 70
국사 점수: 0
과락 과목: 영어, 점수: 59
과락 과목: 국사, 점수: 0

 

 

while문을 이용해서 학급 학생 수가 가장 작은 학급과 가장 많은 학급을 출력해보자.

studentCnts = [[1, 18], [2, 19], [3, 23], [4, 21], [5, 20], [6, 22], [7, 17]]
minClassNo = 0
maxClassNo = 0
minCnt = 0
maxCnt = 0
n = 0
while n < len(studentCnts):
    if minCnt == 0 or minCnt > studentCnts[n][1]:
        minClassNo = studentCnts[n][0]
        minCnt = studentCnts[n][1]
    if maxCnt < studentCnts[n][1]:
        maxClassNo = studentCnts[n][0]
        maxCnt = studentCnts[n][1]
    n += 1
print('학생 수가 가장 적은 학급: {}학급, {}명'.format(minClassNo, minCnt))
print('학생 수가 가장 많은 학급: {}학급, {}명'.format(maxClassNo, maxCnt))
학생 수가 가장 적은 학급: 7학급, 17명
학생 수가 가장 많은 학급: 3학급, 23명

 

'프로그래밍 언어 > Python' 카테고리의 다른 글

리스트 append() 함수(파이썬)  (0) 2023.01.22
리스트 enumerate()함수(파이썬)  (1) 2023.01.22
리스트와 for문(파이썬)  (0) 2023.01.22
리스트 길이(파이썬)  (0) 2023.01.22
리스트 인덱스(파이썬)  (0) 2023.01.22
Comments