1. 조건문
def get_grade(score):
# some code
if score > 90:
return "A"
elif score > 80:
return "B"
elif score > 70:
return "C"
else:
return "F"
score = int(input("점수를 입력 : "))
grade = get_grade(score)
print(grade) # A ~ F
2. 반복문(while)
isdigit 함수를 통해 input된게 숫자인지 아닌지를 걸러내고
exit를 입력하거나(a == "exit") 다섯번 반복할때까지 (if cnt > 4: break) 반복.
뭔가 좀 더 깔끔하게 만들 수 있을거같은데 애매하네..
cnt = 0
while 1:
a = input("문자 혹은 숫자를 입력 : ")
if a == "exit":
print("프로그램을 종료합니다.")
exit()
elif a.isdigit() == True:
print(2*int(a))
cnt += 1
if cnt > 4:
break
elif a.isdigit() == False:
print(f"입력한 문자는 {a}입니다.")
cnt += 1
if cnt > 4:
break
3. 반복문(for) - 진행중
일단 패킹 언패킹 부분 안건드리고 단순하게 구상만 해보자면
users = [
{"name": "Ronald", "age": 30, "math_score": 93, "science_score": 65, "english_score": 93, "social_score": 92},
{"name": "Amelia", "age": 24, "math_score": 88, "science_score": 52, "english_score": 78, "social_score": 91},
{"name": "Nathaniel", "age": 28, "math_score": 48, "science_score": 40, "english_score": 49, "social_score": 91},
{"name": "Sally", "age": 29, "math_score": 100, "science_score": 69, "english_score": 67, "social_score": 82},
{"name": "Alexander", "age": 30, "math_score": 69, "science_score": 52, "english_score": 98, "social_score": 44},
{"name": "Madge", "age": 22, "math_score": 52, "science_score": 63, "english_score": 54, "social_score": 47},
{"name": "Trevor", "age": 23, "math_score": 89, "science_score": 88, "english_score": 69, "social_score": 93},
{"name": "Andre", "age": 23, "math_score": 50, "science_score": 56, "english_score": 99, "social_score": 54},
{"name": "Rodney", "age": 16, "math_score": 66, "science_score": 55, "english_score": 58, "social_score": 43},
{"name": "Raymond", "age": 26, "math_score": 49, "science_score": 55, "english_score": 95, "social_score": 82},
{"name": "Scott", "age": 15, "math_score": 85, "science_score": 92, "english_score": 56, "social_score": 85},
{"name": "Jeanette", "age": 28, "math_score": 48, "science_score": 65, "english_score": 77, "social_score": 94},
{"name": "Sallie", "age": 25, "math_score": 42, "science_score": 72, "english_score": 95, "social_score": 44},
{"name": "Richard", "age": 21, "math_score": 71, "science_score": 95, "english_score": 61, "social_score": 59},
{"name": "Callie", "age": 15, "math_score": 98, "science_score": 50, "english_score": 100, "social_score": 74},
]
for user in users:
if (user["math_score"] + user["science_score"] + user["english_score"] + user["social_score"]) / 4 >= 70:
print(user["age"],user["name"])
# def get_filter_user(users):
# # some code
# return filter_users
#
# filter_users = get_filter_user(users)
# pprint(filter_users)
"""
[{'age': 30, 'name': 'Ronald'},
{'age': 24, 'name': 'Amelia'},
{'age': 29, 'name': 'Sally'},
{'age': 23, 'name': 'Trevor'},
{'age': 26, 'name': 'Raymond'},
{'age': 15, 'name': 'Scott'},
{'age': 28, 'name': 'Jeanette'},
{'age': 21, 'name': 'Richard'},
{'age': 15, 'name': 'Callie'}]
"""
이지만, 당연히 이렇게 만들면
filter 리스트를 만들고 안에 추가하는 형식으로 만들고싶은데
위의 코드는 추가가 아니라 filter가 계속 갱신되는 형태라서 맨 마지막만 출력되는걸 확인할 수 있다.
왜 안되는진 알겠는데 어떻게 되게 만드는질 모르겠네 아 ㅋㅋ
제출본
from pprint import pprint
users = [
{"name": "Ronald", "age": 30, "math_score": 93, "science_score": 65, "english_score": 93, "social_score": 92},
{"name": "Amelia", "age": 24, "math_score": 88, "science_score": 52, "english_score": 78, "social_score": 91},
{"name": "Nathaniel", "age": 28, "math_score": 48, "science_score": 40, "english_score": 49, "social_score": 91},
{"name": "Sally", "age": 29, "math_score": 100, "science_score": 69, "english_score": 67, "social_score": 82},
{"name": "Alexander", "age": 30, "math_score": 69, "science_score": 52, "english_score": 98, "social_score": 44},
{"name": "Madge", "age": 22, "math_score": 52, "science_score": 63, "english_score": 54, "social_score": 47},
{"name": "Trevor", "age": 23, "math_score": 89, "science_score": 88, "english_score": 69, "social_score": 93},
{"name": "Andre", "age": 23, "math_score": 50, "science_score": 56, "english_score": 99, "social_score": 54},
{"name": "Rodney", "age": 16, "math_score": 66, "science_score": 55, "english_score": 58, "social_score": 43},
{"name": "Raymond", "age": 26, "math_score": 49, "science_score": 55, "english_score": 95, "social_score": 82},
{"name": "Scott", "age": 15, "math_score": 85, "science_score": 92, "english_score": 56, "social_score": 85},
{"name": "Jeanette", "age": 28, "math_score": 48, "science_score": 65, "english_score": 77, "social_score": 94},
{"name": "Sallie", "age": 25, "math_score": 42, "science_score": 72, "english_score": 95, "social_score": 44},
{"name": "Richard", "age": 21, "math_score": 71, "science_score": 95, "english_score": 61, "social_score": 59},
{"name": "Callie", "age": 15, "math_score": 98, "science_score": 50, "english_score": 100, "social_score": 74},
]
def get_filter_user(users):
filter_users = []
for user in users:
double_filter_users = {}
if (user["math_score"] + user["science_score"] + user["english_score"] + user["social_score"]) / 4 >= 70: # 평균이 70점 이상 골라내기
double_filter_users["age"] = user["age"]
double_filter_users["name"] = user["name"]
filter_users.append(double_filter_users)
return filter_users
filter_users = get_filter_user(users)
pprint(filter_users)
"""
[{'age': 30, 'name': 'Ronald'},
{'age': 24, 'name': 'Amelia'},
{'age': 29, 'name': 'Sally'},
{'age': 23, 'name': 'Trevor'},
{'age': 26, 'name': 'Raymond'},
{'age': 15, 'name': 'Scott'},
{'age': 28, 'name': 'Jeanette'},
{'age': 21, 'name': 'Richard'},
{'age': 15, 'name': 'Callie'}]
"""
일단 제대로 돌아는 가게 만들었는데 암만봐도 영 깔끔하지가 않다 ㅋㅋ