본문 바로가기

여러가지/Python

Day 5 - for 반복문, shuffle(리스트 섞는 함수)

● 학습 내용

 

1) for 반복문

fruits = ['apple', 'banana', 'orange']
for fruit in fruits:
  print(fruit)

 

for n in range(1, 10):
  print(n)

-> 1 간격으로 1부터 (10-1)숫자까지 출력

for n in range(1, 10, 3):
  print(n)

-> 3 간격으로 1부터 (10-1) 숫자까지 출력

 

ex) 평균 몸무게

student_heights = input().split()
for n in range(0, len(student_heights)):
  student_heights[n] = int(student_heights[n])

num_student = 0
total_height = 0

for height in student_heights:
  total_height += height

for student in student_heights:
  num_student += 1

avg_total = round(total_height / num_student)

print(f"total height = {total_height}")
print(f"number of students = {num_student}")
print(f"average height = {avg_total}")

# round()

 

ex) 가장 높은 점수 찾기

student_scores = input().split()
for n in range(0, len(student_scores)):
  student_scores[n] = int(student_scores[n])

highest_score = 0

for score in range(0, len(student_scores)):
  if student_scores[score] > highest_score:
    highest_score = student_scores[score]

print(f"The highest score in the class is: {highest_score}")
for score in student_scores:
  if score > highest_score:
    highest_score = score
    
print(f"The highest score in the class is: {highest_score}")

 

ex) target까지의 모든 짝수의 합

target = int(input())

even_sum = 0

for n in range(0, target+1, 2):
  even_sum += n

print(even_sum)

 

ex)

for n in range(1,101):
  if n % 3 == 0 and n % 5 == 0:
    print("FizzBuzz")
  elif n % 3 == 0:
    print("Fizz")
  elif n % 5 == 0:
    print("Buzz")
  else:
    print(n)

 

 

(랜덤 비밀번호 생성기)

● GIT URL : https://github.com/yooyijun15/Python/blob/main/Day5/main.py

 

Python/Day5/main.py at main · yooyijun15/Python

Contribute to yooyijun15/Python development by creating an account on GitHub.

github.com

# shuffle: 리스트 섞는 함수

=> 문자열은 섞을 수 없으므로 리스트로 작업 후, 문자열로 변경