为什么我对mod (%) 的2位数字return 的操作在python 中为零

Why does my operation of mod (%) on a 2 digit number return zero in python

作为一个完全的初学者,我一直在尝试尝试一些 leetcode 问题,其中一节要求我 convert/return 数字的千位、百位、十位和个位。

这是python中的代码。

class Solution:
    def intToRoman(self, num: int) -> str:
        list = {1 :   'I',
                4 :   'IV',
                5 :   'V',
                9 :   'IX',
                10:   'X',
                40:   'XL',
                50:   'L',
                90:   'XC',
                100:  'C',
                400:  'CD',
                500:  'D',
                900:  'CM',
                1000: 'M'}

        thousands, hundreds, tens, unit = 0,0,0,0
        string = ""
        number = int(num)
        
        #A Thousandth number
        
        if number >= 1000:
            thousands = number - (number % 1000)              # 3549 - (3549 % 1000) = 3000

            hundreds = number - thousands;                  #// 3549 - 3000 = 549
            tens = hundreds % 100                      #// 549 % 100 = 49
            hundreds = hundreds - (hundreds % 100)     #// 549 - (549 % 100) = 500

            units = tens % 10
            tens = tens - (tens % 10)                  #// 49 - (49 % 10) = 49 - 9 = 9

            thou = thousands // 1000                     # 3

            #THOUSANDS
            for i in range(thou):
                string = string + (list[1000])

            #HUNDREDS
            hund = hundreds // 100

            if hund < 4:
                string = string +(hund * list[100])
                
            elif hund == 4:
                string = string +(list[400])
                
            elif hund == 5:
                string = string +(list[500])
                
            elif hund >= 6 and hund <= 8:
                string = string +(list[500])
                hund = hund - 5
                string = string +(hund * list[100])
                
            elif hund == 9:
                string = string +(list[900])
                
            #TENS
            ten = tens // 10

            if ten < 4:
                string = string +(ten * list[10])

            elif ten == 4:
                string = string +(list[40])

            elif ten == 5:
                string = string +(list[50])

            elif ten >= 6 and ten <= 8:
                string = string +(list[50])
                ten = ten - 5
                string = string +(ten * list[10])

            elif ten == 9:
                string = string +(list[90])
                
            #UNITS
            if unit < 4:
                string = string +(unit * list[1])
                
            elif unit == 5:
                string = string +(list[5])
                
            elif unit >= 6 and unit <= 8:
                string = string +(list[5])
                unit = unit - 5
                string = string +(unit * list[1])
            elif unit == 9:
                string = string +(list[9])

            return string

现在,我的问题是 Units 变量保持为零。我不明白为什么?翻译成 C 时,这个确切的代码块可以正常工作。 python 中的 Mod 是否有我遗漏的有趣之处?

例如:如果输入数字是 3549(必须将整数转换为罗马数字),units 变量保持为零。输出应为 MMMDXLIX。

由于输出中的 IX 取决于单位变量(由于某种原因保持为零),因此输出为 MMMDXL

顺便说一句,该函数仅适用于 >= 1000 的数字。我没有进一步编码。

您在这些代码块中有错字:

        if number >= 1000:
            thousands = number - (number % 1000)              # 3549 - (3549 % 1000) = 3000

            hundreds = number - thousands;                  #// 3549 - 3000 = 549
            tens = hundreds % 100                      #// 549 % 100 = 49
            hundreds = hundreds - (hundreds % 100)     #// 549 - (549 % 100) = 500

            unit = tens % 10 # you input "units" here
            tens = tens - (tens % 10)                  #// 49 - (49 % 10) = 49 - 9 = 9

            thou = thousands // 1000                     # 3

结果:

MMMDXLIX

顺便说一句,这个问题有更好的解决方案。

我的代码:

def intToRoman(num: int) -> str:
    roman_dict_key = [1,4,5,9,10,40,50,90,100,400,500,900,1000][::-1]
    roman_dict_value = ["I","IV","V","IX","X","XL","L","XC","C","CD","D","CM","M"][::-1]
    roman_dict = dict(zip(roman_dict_key,roman_dict_value))
    ans = ""
    for digit,roman in roman_dict.items():
        while num>=digit:
            num -= digit
            ans += roman
    return ans