Python: 데이터형식 + 대화형코딩 과제

Frigate·2022년 5월 20일
0

Python 기초문법

목록 보기
14/27

학습목표: 대화형 코딩연습 + 데이터형식

참고자료: https://replit.com/@appbrewery/day-2-1-exercise

Instuctions.

두자리수를 입력하면, 앞자리 수 + 뒷자리 수 를 출력하는 프로그램을 짜시오

  • 2자리수 유형이 뭔지 생각해보기

  • 문자열에서 특정 글자를 추출하는 Sub-Scripting 을 고려하시오

  • 형 변환을 고려하시오.

Write a program that adds the digits in a 2 digit number.

e.g. if the input was 35, then the output should be 3 + 5 = 8

Warning. Do not change the code on lines 1-3. Your program should work for different inputs. e.g. any two-digit number. (check out 참고자료)

Example Input

39

Example Output

# 3+9=12
12



오답1

  • 23을 입력받고 서브스크립팅 기법으로 앞자리, 뒷자리 추출해서 print 함수로 더해줬는데

    추출한걸 str 으로 인식했음;

    input()함수가 입력받은건 두자리 수이긴 한데, 데이터 타입 명시해둔게 없어서, 기본값인 str로 인식한듯 함.

    때문에 서브스크립팅한 앞자리, 뒷자리수를 정수Int 타입변환 필요성이 생김.

two_digit_number = input("Type a two digit number: ")
print(int(two_digit_number[0]) + int(two_digit_number[1]))   #int형으로 형변환 시켜줌 ㅋ

#실행결과:
Type a two digit number: 16
7

오답2

  • 정수형으로 형변환할때 대문자로 Int 해줘서 에러남.. int 소문자로 써줘야했음...
two_digit_number = input("Type a two digit number: ")f
irst_two_digit_number = two_digit_number[0]
last_two_digit_number = two_digit_number[1]
first_one = Int(first_two_digit_number)  #대문자를 소문자 int로 수정필요
last_one = Int(last_two_digit_number)
print(first_one + last_one)



답1

# 🚨 Don't change the code below 👇
two_digit_number = input("Type a two digit number: ")
# 🚨 Don't change the code above 👆

result = int(two_digit_number[0]) + int(two_digit_number[1])
print(result)
####################################
#First *fork* your copy. Then copy-paste your code below this line 👇
#Finally click "Run" to execute the tests

해설:

two_digit_number 에는 타입제한 없이 데이터가 할당될 수 있다

input 지시문에서 2자리수 입력하라고 했으니, 사용자는 2자리수를 입력할것이다.

입력받은 2자리수의 앞자리와 뒷자리를 서브스크립팅[ ] 으로 추출하고

형변환 int로 시켜주고, + 연산으로 결과를 출력하면 된다.


답2:

first_two_digit_number = two_digit_number[0]
last_two_digit_number = two_digit_number[1]
first_one = int(first_two_digit_number)
last_one = int(last_two_digit_number)
print(first_one + last_one)

#유데미 #유데미코리아 #스타트위드유데미 #스터디윗미

profile
Swift

0개의 댓글