without haste but without rest

[python/계프] 2주차 과제 - 12진수 변환 본문

Homework

[python/계프] 2주차 과제 - 12진수 변환

JinungKim 2020. 4. 4. 16:10

10진수, 12진수 변환

def tenToTwe(n):
    tempList = []
    check = n
    while(1):
        letter = check % 12
        check = check // 12
        if letter == 10:
            res = 'a'
        elif letter == 11:
            res = 'b'
        else:
            res = str(letter)
        tempList.append(res)
        if check == 0:
            break

    tempList.reverse()
    result = ''.join(tempList)
    return result


def tweToTen(n):
    tempList = list(n)
    resList = []
    res = 0
    for a in tempList:
        if a == 'a':
            temp = 10
        elif a == 'b':
            temp = 11
        elif a.isdigit():
            temp = int(a)
        else:
            print('12진수에는 a, b만 사용할 수 있습니다.')
            return
        resList.append(temp)

    for a in range(len(resList)):
        if a == len(resList)-1:
            res += resList[a]
        elif a == 0:
            res = resList[0] * 12
        else:
            res = (res + resList[a]) * 12
    return res


while(1):
    n = int(input("10진수 => "))
    if n == 0:
        print("안녕히 가세요.")
        break
    print(n, '(10) => ', tenToTwe(n), '(12)')
    twe = input("12진수 => ")
    if tweToTen(twe):
        print(twe, '(12) => ', tweToTen(twe), '(10)')

 

Comments