Pythonはデジタル変換通貨を実現

18630 ワード

  • のような対応関係が多い場合は,辞書データを用いてifの判断を減らすことができる.
  • amount_chinese = {1: ' ', 2: ' ', 3: ' ', 4: ' ', 5: ' ', 6: ' ', 7: ' ', 8: ' ', 9: ' ', 10: ' ', 100: ' ', 1000: ' ',
                      10000: ' ', 100000000: ' ', 15: ' ', 16: ' ', 17: ' ', 0: ' ', 19: ' '}
    place = {1: 1, 2: 10, 3: 100, 4: 1000}
    
    
    def write_to_chinese(num):
        new_str = ""
        num_len = len(str(num))
        while True:
            if num_len == 1:
                if num != 0:
                    new_str += amount_chinese.get(num)
                break
            if num % place.get(num_len) == 0:
                new_str += amount_chinese.get(int(str(num)[0])) + amount_chinese.get(num)
                break
            if num_len in place:
                new_str += amount_chinese.get(num // place.get(num_len))
                position = amount_chinese.get(place.get(num_len))
                new_str += position
            num = num % place.get(num_len)
            if num_len - len(str(num)) > 1:
                new_str += amount_chinese.get(0)
            num_len = len(str(num))
        return new_str
    
    while True:
        try:
            num_list = input().split(".")
            new_num = 0
            string = ""
            point_num = None
            if len(num_list) == 2:
                point_num = num_list[1]
            num = int(num_list[0])
            if 15>=len(str(num)) >12:
                new_num = num // 100000000000
                num = num % 100000000000
                string += write_to_chinese(new_num)
                string += amount_chinese.get(10000)
                string += amount_chinese.get(100000000)
    
            if 13>len(str(num)) >=9:
                new_num = num // 100000000
                num = num % 100000000
                string += write_to_chinese(new_num)
                string += amount_chinese.get(100000000)
    
            if 9 > len(str(num)) >= 5:
                new_num = num // 10000
                num = num % 10000
                string += write_to_chinese(new_num)
                string += amount_chinese.get(10000)
            string += write_to_chinese(num)
            if not point_num:
                string += amount_chinese.get(15) + amount_chinese.get(19)
            elif  len(point_num)  == 2 and point_num[0] == point_num[1] =="0":
                string += amount_chinese.get(19)
    
            else:
                if num !=0:
                    string += amount_chinese.get(15)
                for i in range(len(point_num)):
                    if point_num[i] =="0":
                        continue
                    string += amount_chinese.get(int(point_num[i]))
                    if i ==0:
                        string += amount_chinese.get(16)
                    else:
                        string += amount_chinese.get(17)
            string = string.replace("  "," ")
            print("   "+string)
    
        except:
            break