Day 005

AWESOMee·2022년 1월 26일
0

Udemy Python Bootcamp

목록 보기
5/64
post-thumbnail

Udemy Python Bootcamp Day 005

For loop

for item in list_of_items:
Do something to each item

fruits = ["Apple", "Peach", "Pear"]
for fruit in fruits:
  print(fruit)
#taking this list og fruits ans signing a variable name, fruit, to each of them
  print(fruit + "pie")
  #indentation is really important
  print(fruits)

#Output
Apple
Applepie
['Apple', 'Peach', 'Pear']
Peach
Peachpie
['Apple', 'Peach', 'Pear']
Pear
Pearpie
['Apple', 'Peach', 'Pear']

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

#Output
Apple
Applepie
Peach
Peachpie
Pear
Pearpie
['Apple', 'Peach', 'Pear']

list average 구하기

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])

#내가 쓴 답
total_height = 0
people = 0
for height in student_heights:
  total_height += height
  people += 1
print(round(total_height / people))

#sum(), len()을 이용하여 답 풀이
total_height = sum(student_heights)
number_of_students = len(student_heights)
average_height = round(total_height / number_of_students)
print(average_height)

#함수 이용하지 않고 for loop로 푸는 법
total_height = 0
for height in student_heights:
  total_height += height

number_of_students = 0
for student in student_heights:
  number_of_students += 1

average_height = round(total_height / number_of_students)
print(average_height)

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)

#함수 이용해서 구하기
print(max(student_scores))
  #print maximum score
print(min(student_scores))
  #print minimum score

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

For loop with Range

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

#output
1
2
3
4
5
6
7
8
9

for number in range(1, 11, 3):
  print(number)

#output
1
4
7
10

total = 0
for number in range(1, 101):
  total += number
print(total)

#output
5050

FizzBuzz

for number in range(1, 101):
  if number % 3 == 0 and number % 5 == 0:
    #the order is important
    print("FizzBuzz")
  elif number % 3 == 0:
    print("Fizz")
  elif number % 5 == 0:
    print("Buzz")
  else:
    print(number)

Password Generator Project

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"))

#Eazy Level - Order not randomised:
#e.g. 4 letter, 2 symbol, 2 number = JduE&!91
password = ""

for char in range(1, nr_letters + 1):
  password += random.choice(letters)

for char in range(1, nr_numbers + 1):
  password += random.choice(numbers)

for char in range(1, nr_symbols + 1):
  password += random.choice(symbols)

print(password)

#Hard Level - Order of characters randomised:
#e.g. 4 letter, 2 symbol, 2 number = g^2jk8&P

password_list = []

for char in range(1, nr_letters + 1):
  password_list.append(random.choice(letters))

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

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

print(password_list)
#.suffle() function
random.shuffle(password_list)
print(password_list)

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

print(f"Your password is: {password}")
profile
개발을 배우는 듯 하면서도

0개의 댓글