본문 바로가기
개발일지/Python

[복기 겸 유용] 파이썬 절대값 함수 abs

by 개발에정착하고싶다 2022. 4. 30.
320x100
# 해답코드
# 시작부터 나열하는 거에 있어서 ; 로 이어붙이는 거를 누락했기에 내 코드는 줄이 불필요하게 길어졌다.


korAvg = 85; engAvg = 82; matAvg = 89
sciAvg = 75; hisAvg = 94
totalAvg = korAvg + engAvg + matAvg + sciAvg + hisAvg
avgAvg = int(totalAvg / 5)

korScore = int(input('국어 점수: '))
engScore = int(input('영어 점수: '))
matScore = int(input('수학 점수: '))
sciScore = int(input('과학 점수: '))
hisScore = int(input('국사 점수: '))

totalScore = korScore + engScore + matScore + sciScore + hisScore
avgScore = int(totalScore / 5)

korGap = korScore - korAvg
engGap = engScore - engAvg
matGap = matScore - matAvg
sciGap = sciScore - sciAvg
hisGap = hisScore - hisAvg

totalGap = totalScore - totalAvg
avgGap = avgScore - avgAvg

print('-'*50)
print('총점: {}({}), 평균: {}({})'.format(totalScore,totalGap,avgScore,avgGap))
print('국어: {}({}), 영어: {}({}), 수학: {}({}), 과학: {}({}), 국사: {}({})'.format(
    korScore,korGap,engScore,engGap,matScore,matGap,sciScore,sciGap,hisScore,hisGap
))
print('-'*50)


# 정말 중요하다. 절대값 변경 함수 abs
# 본 문제의 경우에 왜 else가 출력이 안되었나 생각해보니깐 - 이하의 숫자는 *와같은 문자 출력을 할때 효력을 못내는것같다.
# 때문제 음수는 절대값으로 변경해 줘야하는데 이때 사용해야하는게 abs

str = '+' if korGap > 0 else '-'
print('국어 편차: {}({})'.format(str * abs(korGap), korGap))
str = '+' if engGap > 0 else '-'
print('영어 편차: {}({})'.format(str * abs(engGap), engGap))
str = '+' if matGap > 0 else '-'
print('수학 편차: {}({})'.format(str * abs(matGap), matGap))
str = '+' if sciGap > 0 else '-'
print('과학 편차: {}({})'.format(str * abs(sciGap), sciGap))
str = '+' if hisGap > 0 else '-'
print('국사 편차: {}({})'.format(str * abs(hisGap), hisGap))
str = '+' if totalGap > 0 else '-'
print('총점 편차: {}({})'.format(str * abs(totalGap), totalGap))
str = '+' if avgGap > 0 else '-'
print('평균 편차: {}({})'.format(str * abs(avgGap), avgGap))

print('-'*50)

 

 

아래가 내가 작성했던 코드다.

 

 

# 절대값 함수 abs 응용 버전


# 내 코드
# 각 편차에 따른 막대그래프를 생성하는데 있어서 + 생성은 문제가 없지만 else문 작동이 안되더라.
# 원인은 else문 실행 여부에 있으며. 아무튼 실패

standard_kor_score = 85
standard_eng_score = 82
standard_math_score = 89
standard_science_score = 75
standard_history_score = 94

standard_total_score = standard_history_score + standard_science_score + \
                       standard_math_score + standard_eng_score + standard_kor_score

standard_avg_score = int(standard_total_score / 5)

kor_score = int(input('국어 점수를 입력하세요: '))
eng_score = int(input('영어 점수를 입력하세요: '))
math_score = int(input('수학 점수를 입력하세요: '))
science_score = int(input('과학 점수를 입력하세요: '))
history_score = int(input('국사 점수를 입력하세요: '))

total_score = kor_score + eng_score + math_score + science_score + history_score
avg_score = int(total_score / 5)

compare_kor = kor_score - standard_kor_score
compare_eng = eng_score - standard_eng_score
compare_math = math_score - standard_math_score
compare_science = science_score - standard_science_score
compare_history = history_score - standard_history_score
compare_total = total_score - standard_total_score
compare_avg = avg_score - standard_avg_score

print('-'*30)
print('총점: {}({}),\t평균: {}({})'.format(total_score,
                                       compare_total,avg_score,compare_avg))

print('국어: {}({}),\t영어: {}({}),\t수학: {}({}),\t과학: {}({}),\t국사: {}({})'.format(kor_score,
                                                                              compare_kor,eng_score,compare_eng,
                                                                              math_score,compare_math,science_score,
                                                                              compare_science, history_score,
                                                                              compare_history))

print('-'*30)

print('국어 편차:',end='')
print('+'* compare_kor,f'({compare_kor})') if compare_kor > 0 else print('-' * compare_kor,f'({compare_kor})')
print('영어 편차:',end='')
print('+'* compare_eng,f'({compare_eng})') if compare_eng > 0 else print('-' * compare_eng,f'({compare_eng})')
print('수학 편차:',end='')
print('+'* compare_math,f'({compare_math})') if compare_math > 0 else print('-' * compare_math,f'({compare_math})')
print('과학 편차:',end='')
print('+'* compare_science,f'({compare_science})') if compare_science > 0 else print('-' * compare_science,f'({compare_science})')
print('국사 편차:',end='')
print('+'* compare_history,f'({compare_history})') if compare_history > 0 else print('-' * compare_history,f'({compare_history})')
print('총점 편차:',end='')
print('+'* compare_total,f'({compare_total})') if compare_total > 0 else print('-' * compare_total,f'({compare_total})')
print('평균 편차:',end='')
print('+'* compare_avg,f'({compare_avg})') if compare_avg > 0 else print('-' * compare_avg,f'({compare_avg})')
300x250