I am planning to create the next program for Python's university assignment
For integers from 1 to 9999, we have created a function called thousand()
that returns the kanji notation.
Expected Execution Example:
thousands (9876) → "98,876"
thousands(12) → "12"
thousands(102) → "102"
I try to divide it into cases such as a thousand, a hundred, and so on and add it to an empty list, but I can't do it well only a thousand.I'm writing the code in a similar way to other places, but I don't understand why it doesn't work.
My code
def thousands(n):
US>"s="
a = str(n)
iflen(a) == 4:
d=a[-4]
list3 = [", "Sen", "2,000", "3,000", "4,000", "5,000", "6,000", "7,000", "8,000", "9,000"]
for i in range (0,9):
if int(d) == 0:
s+=list3[0]
elifint(d)==i:
s+=list3[i]
iflen(a)>=3:
e=a[-3]
list 4 = [", "100", "200", "300", "400", "500", "600", "700", "800", "900"]
for jin range(0,9):
if int(e) == 0:
s+=list4[0]
elifint(e) == j:
s+=list4[j]
iflen(a)>=2:
f = a [-2]
list 5 = [", "10", "20", "30", "40", "50", "60", "70", "80", "90"]
fork in range (0,9):
if int(f) == 0:
s+=list5[0]
elifint(f)==k:
s+=list5[k]
iflen(a)>=1:
g=a[-1]
list1 = ["", "1", "2", "3", "4", "5", "6", "7", "8", "9"]
for line range (0,9):
if int(g) == 0:
s+=list1[0]
elifint(g) == l:
s+=list1[l]
returns
The range
range is causing the 9
processing to be excluded from the list of thousands.
For example, print(thousands(192)
prints 102
.
range
returns consecutive numbers that do not include the second argument
>>>list(range(0,9))
[0, 1, 2, 3, 4, 5, 6, 7, 8]
As mentioned above, 9
is not included in the loop, so if you rewrite it to range(0,10)
, the correct value will be printed.
© 2024 OneMinuteCode. All rights reserved.