[udemy] python 부트캠프 _ section 5_ 파이썬 반복문

Dreamer ·2022년 8월 9일
0
post-thumbnail

1. 파이썬 리스트로 for 반복문 사용하기

fruits = ["Apple","Peach","Pear"]
for fruit in fruits:
    print(fruit)
    print(fruit + "Pie")

2. quiz _ average height

student_heights = input("Input a list of student heights ").split()
for n in range(0, len(student_heights)):
  student_heights[n] = int(student_heights[n])

i=0
sum=0
for i in range(0,len(student_heights)):
  sum+=student_heights[i]
  ave = sum / int(len(student_heights)) 
print(round(ave))

3. highest score

student_scores = input("Input a list of student scores ").split()

for n in range(0, len(student_scores)):
  student_scores[n] = int(student_scores[n])
print(student_scores)

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

4. for 반복문과 range() 함수

for number in range(1,10):
    print(number)
  • range(a,b) : 특정 숫자 사이의 범위 사이의 숫자를 추출. (a~ b-1)
  • range(a,b,c) : a~ b-1 사이의 숫자를 c 간격만큼 추출
total = 0
for number in range(1,101)
    total += number
print(total)

5. quiz _ adding evens

sum = 0
for i in range(0,101,2):
  sum +=i
print(sum)

#2.
total2 = 0
for number in range(1,101):
  if number % 2==0:
    total2 +=number
print(total2)

6. FizzBuzz 면접 문제

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

7. project _ password generator!

import random
letters = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']
numbers = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
symbols = ['!', '#', '$', '%', '&', '(', ')', '*', '+']

print("Welcome to the PyPassword Generator!")
nr_letters= int(input("How many letters would you like in your password?\n")) 
nr_symbols = int(input(f"How many symbols would you like?\n"))
nr_numbers = int(input(f"How many numbers would you like?\n"))


select_letter = random.choices(letters, k = nr_letters)
select_symbols = random.choices(symbols, k = nr_symbols)
select_numbers = random.choices(numbers, k = nr_numbers)

password = (select_letter+select_symbols+select_numbers)
random.shuffle(password)
print(f"Your password is {''.join(password)}")
password_list = []
for char in range(1,nr_letters+1):
  password_list.append(random.choice(letters))

for sym in range(1,nr_symbols+1):
  password_list.append(random.choice(symbols))

for num in range(1,nr_numbers):
  password_list.append(random.choice(numbers))

random.shuffle(password_list)

password = ""
for char in password_list:
  password += char

print(f"Your password is {password}")
profile
To be a changer who can overturn world

0개의 댓글