#에러 코드 weight = int(input('체중 입력(g):')) height = int(input('신장 입력(cm):')) if weight.isdigit(): weight = weight / 10 if height.isdigit(): height = height / 100 print('체중:{}kg'.format(weight)) print('신장:{}m'.format(height)) bmi = weight / (height * height) print('BMI: %f' % bmi) #실행결과 AttributeError: 'int' object has no attribute 'isdigit'
AttributeError는 객체 혹은 클래스에 사용할 수 없는 속성을 사용하는 경우 발생한다고 합니다.
위의 코드에서는 weight 과 height에 바로 int를 적용시켜 정수화 됬으니 isdigit()함수를 대입할 수 가 없다고 이해하였습니다.
따라서 int를 빼주어 다시 써보면 에러가 안뜨고 정상 작동하는 것을 알 수 있었습니다.
weight = input('체중 입력(g):') height = input('신장 입력(cm):') if weight.isdigit(): weight = int(weight) / 10 if height.isdigit(): height = int(height) / 100 print('체중:{}kg'.format(weight)) print('신장:{}m'.format(height)) bmi = weight / (height * height) print('BMI: %.2f' % bmi) #실행결과 신장 입력(cm):187 체중:85.5kg 신장:1.87m BMI: 24.45
num1=10 num2=20 print('num1:{},num2:{}'.format(num1,num2)) num1 = 20 num2 = 10 print('num1:{},num2:{}'.format(num1,num2)) #실행결과 num1:10,num2:20 num1:20,num2:10
midExamScore = input('중간 고사 점수:') finalExamScore = input('기말 고사 점수:') if midExamScore.isdigit(): midExamScore = int(midExamScore) print('중간 고사 점수:{}'.format(midExamScore)) else: print('잘못입력하셨습니다.') if finalExamScore.isdigit(): finalExamScore = int(finalExamScore) print('기말 고사 점수:{}'.format(finalExamScore)) else: print('잘못입력하셨습니다') sum = midExamScore+finalExamScore avg = sum / 2 print('총점:{}'.format(sum)) print('평균:{}'.format(avg)) #실행결과 중간 고사 점수:80 기말 고사 점수:90 총점:170 평균:85.0
chooseYourLanguage = int(input('언어 선택 (1. 한국어, 2.English) :')) if chooseYourLanguage == 1: print('1.샌드위치\t 2.햄버거 \t 3.쥬스') if chooseYourLanguage == 2: print('1. sandwich\t 2.Hamburger\t 3.Juice')
myage = int(input('나의 나이는?:')) age100 = 100 - myage year = 2023 print(year+age100)
import datetime today=datetime.datetime.today() myage=input('나의 나이는?:') if myage.isdigit(): afterage=100-int(myage) my100=today.year + afterage print('{}년 ({}년후)에 100살이 됩니다'.format(my100,afterage))
money50000 = 50000;money10000 = 10000;money5000 =5000;money1000=1000;money500=500;money100=100;money10=10 goods = int(input('상품 가격 입력:')) money = int(input('지불 금액:')) if money > goods: changes = money - goods changes = (changes // 10 ) * 10 print('거스름돈:{}'.format(changes)) print('-'*30 ) if changes > 50000: changesCnt = changes // money50000 print('50,000 {}장'.format(changesCnt)) changes = changes % money50000 if changes > 10000: changesCnt = changes // money10000 print('10,000 {}장'.format(changesCnt)) changes = changes % money10000 if changes > 5000: changesCnt = changes // money5000 print('5,000 {}장'.format(changesCnt)) changes = changes % money5000 if changes > 1000: changesCnt = changes // money1000 print('1,000 {}장'.format(changesCnt)) changes = changes % money1000 if changes > 500: changesCnt = changes // money500 print('500 {}개'.format(changesCnt)) changes = changes % money500 if changes > 100: changesCnt = changes // money100 print('100 {}개'.format(changesCnt)) changes = changes % money100 if changes > 10: changesCnt = changes // money10 print('10 {}개'.format(changesCnt)) #실행결과 상품 가격 입력:8302 지불 금액:100000 거스름돈:91690 ------------------------------ 50,000 1장 10,000 4장 1,000 1장 500 1개 100 1개 10 9개
kor = '국어'; eng = '영어'; mat = '수학' korScore = int(input('국어 점수 입력:')) engScore = int(input('영어 점수 입력:')) matScore = int(input('수학 점수 입력:')) sum = korScore + engScore + matScore print('총점:{}'.format(korScore+engScore+matScore)) avg = sum / 3 print('평균:{:.2f}'.format(avg)) print('-'*30) maxScore = korScore maxSubject = kor if engScore > maxScore: maxScore = engScore maxSubject = eng if matScore > maxScore: maxScore = matScore maxSubject = mat print('최고 점수 과목:{}({})'.format(maxScore,maxSubject)) minScore = korScore minSubject = kor if engScore < minScore: minScore = engScore minSubject = eng if matScore < minScore: minScore = matScore minSubject = mat print('최저 점수 과목:{}({})'.format(minScore,minSubject)) print('최고 최저 점수 차이:{}'.format(maxScore-minScore)) #실행결과 국어 점수 입력:80 영어 점수 입력:90 수학 점수 입력:100 총점:270 평균:90.00 ------------------------------ 최고 점수 과목:100(수학) 최저 점수 과목:80(국어) 최고 최저 점수 차이:20
message = input('메시지 입력:') tax50 = 50 tax100 = 100 if len(message) < 50: print('SMS 발송!!') print('메시지 길이:{}'.format(len(message))) print('메시지 발송 요금:{}원'.format(tax50)) else: print('MMS 발송!!') print('메시지 길이:{}'.format(len(message))) print('메시지 발송 요금:{}원'.format(tax100)) #실행결과 메시지 입력:안녕하세요 SMS 발송!! 메시지 길이:5 메시지 발송 요금:50원
kor = int(input('국어점수:')) eng = int(input('영어점수:')) mat = int(input('수학점수:')) sci = int(input('과학점수:')) hist = int(input('국사점수:')) sum = kor+eng+mat+sci+hist avg = int(sum/5) korAvg= 85;engAvg=82;matAvg=89;sciAvg=75;histAvg=94 sumAvg=korAvg+engAvg+matAvg+sciAvg+histAvg avgDevi=int(sumAvg/5) korDevi = kor-korAvg engDevi = eng-engAvg matDevi = mat-matAvg sciDevi = sci-sciAvg histDevi = hist-histAvg print('총점:{}({})'.format(sum,(sum-sumAvg))) print('평균:{}({})'.format(avg,(avg-avgDevi))) print('-'*50) str = '+' if korDevi > 0 else '-' print('국어 편차:{}({})'.format(str * abs(korDevi),korDevi)) str = '+' if engDevi > 0 else '-' print('영어 편차:{}({})'.format(str * abs(engDevi),engDevi)) str = '+' if matDevi > 0 else '-' print('수학 편차:{}({})'.format(str * abs(matDevi),matDevi)) str = '+' if sciDevi > 0 else '-' print('과학 편차:{}({})'.format(str * abs(sciDevi),sciDevi)) str = '+' if histDevi > 0 else '-' print('국사 편차:{}({})'.format(str * abs(histDevi),histDevi)) print('-'*50) #실행결과 국어점수:80 영어점수:90 수학점수:100 과학점수:70 국사점수:85 총점:425(0) 평균:85(0) -------------------------------------------------- 국어 편차:-----(-5) 영어 편차:++++++++(8) 수학 편차:+++++++++++(11) 과학 편차:-----(-5) 국사 편차:---------(-9) --------------------------------------------------
for j in range(1,6): for i in range(j): print('*',end='') print() #실행결과 * ** *** **** ***** for j in range(6,0,-1): for i in range(j): print('*',end='') print() #실행결과 ****** ***** **** *** ** * for j in range(1,6): for i in range(j): print('*',end='') print() for j in range(6,0,-1): for i in range(j): print('*',end='') print() #실행결과 * ** *** **** ***** ****** ***** **** *** ** * for i1 in range(1, 6): for i2 in range(6-i1-1): print(' ', end='') for i3 in range(i1): print('*', end='') print() #실행결과 * ** *** **** *****

현재 come on 의 c 도 안왔음 (파이썬 4일차)