딕셔너리의 value 변환하기
2022. 11. 27. 12:21ㆍTrip to Python
점수 기준에 따라서 해당하는 평가 문구를 대입하는 것으로 솔직히 골머리를 앓았다. 언제 코딩이 늘 것인가.....
<내가 짯던 잘못 쓴 코드>
student_scores = {
"Harry": 81,
"Ron": 78,
"Hermione": 99,
"Draco": 74,
"Neville": 62,
}
# 🚨 Don't change the code above 👆
#TODO-1: Create an empty dictionary called student_grades.
student_grades = {}
#TODO-2: Write your code below to add the grades to student_grades.👇
for score in student_scores:
if student_scores["score"] >= 91:
student_scores["score"] = "Outstanding"
elif student_scores["score"] >= 81:
student_scores["score"] = "Exceeds Expectations"
elif student_scores["score"] >= 71:
student_scores["score"] = "Acceptable"
else:
student_scores["score"] = "Fail"
student_grades += student_scores["score"]
- 내가 가장 실수한 부분은 student_score를 기준으로 분류를 하여 student_grades에 넣었어야 하나 student_scores에 배정하는 실수를 저질렀다. 게다가 안에 변수에 "" 넣어 문자열로 인식하게 했다는 점 또한 개선해야할 부분이다.
<정답 코드>
student_scores = {
"Harry": 81,
"Ron": 78,
"Hermione": 99,
"Draco": 74,
"Neville": 62,
}
# 🚨 Don't change the code above 👆
#TODO-1: Create an empty dictionary called student_grades.
student_grades = {}
#TODO-2: Write your code below to add the grades to student_grades.👇
for student in student_scores:
score = student_scores[student]
if score >= 91:
student_grades[student] = "Outstanding"
elif score >= 81:
student_grades[student] = "Exceeds Expectations"
elif score >= 71:
student_grades[student] = "Acceptable"
else:
student_grades[student] = "Fail"
# 🚨 Don't change the code below 👇
print(student_grades)
- 실상 새로운 딕셔너리를 만드는 것이지 value 변환은 아닌 것 같다.
'Trip to Python' 카테고리의 다른 글
(파이썬) 경매 프로그램 만들기 (0) | 2022.12.05 |
---|---|
딕셔너리와 반복문의 활용 (0) | 2022.11.27 |
리스트에 딕셔너리 추가하는 함수 만들기 (0) | 2022.11.27 |
리스트 원소를 어떻게 변경할 것인가? (0) | 2022.11.22 |
클래스(Class) (0) | 2022.11.18 |