使用函数进行转换的问题
lowchars = "1234567890"upchars = "一二三四五六七八九零"
num =input("请输入一个数字:" )
def getnum(j):
index = lowchars.find(j)
print(index)
return upchars
res = getnum(num)
print(num,'的大写是',res)
代码能运行,但不是我想要的结果。我希望的是输入任意位数的数字都能转换成对应的大写数字,不考虑百千万等读数。
现在的代码只能转换一位数字,如何才能转换任意位数的数字,,求大佬指教
{:5_105:}
lowchars = "1234567890"
upchars = "一二三四五六七八九零"
num =input("请输入一个数字:" )
for x in num:
if x in lowchars:
print(upchars,end="") num_dict = {
"1":"一",
"2":"二",
"3":"三",
"4":"四",
"5":"五",
"6":"六",
"7":"七",
"8":"八",
"9":"九",
"0":"零"
}
num =input("请输入数字:")
def getnum(j):
bigNum = ""
for i in j:
bigNum += num_dict
return bigNum
res = getnum(num)
print(num,'的大写是',res) wp231957 发表于 2022-7-11 16:32
lowchars = "1234567890"
upchars = "一二三四五六七八九零"
num = input("请输入一个数字:")
def getnum():
for x in num: #遍历输入的数字num
if x in lowchars:
print(upchars, end="") # 打印x索引对应的大写数字
return upchars
getnum()
为什么放在函数里就不行呢还是只能读取一位数字。断点执行多位数字只能执行第一位,请指教 smyiuo_11 发表于 2022-7-11 19:13
lowchars = "1234567890"
upchars = "一二三四五六七八九零"
num = input("请输入一个数字:")
lowchars = "1234567890"
upchars = "一二三四五六七八九零"
num = input("请输入一个数字:")
for x in num:
if x in lowchars:
def getnum():
return upchars
getnum()
print(upchars, end=' ')
终于解决了,函数要放在循环体内, smyiuo_11 发表于 2022-7-11 19:31
lowchars = "1234567890"
upchars = "一二三四五六七八九零"
num = input("请输入一个数字:")
你还是不了解函数是咋个用法:给你一个DEMO
def foo(s):
lowchars = "1234567890"
upchars = "一二三四五六七八九零"
r=""
for x in s:
if x in lowchars:
r+= upchars
return r
num = input("请输入一个数字:")
print(foo(num)) 临时号 发表于 2022-7-11 19:01
把列表换成字典确实方便了许多,
bigNum = ""
for i in j:
bigNum += num_dict
这里理解起来有点难 smyiuo_11 发表于 2022-7-11 19:31
lowchars = "1234567890"
upchars = "一二三四五六七八九零"
num = input("请输入一个数字:")
还有一个思路,比这个INDEX要好一些
def foo(s):
lowchars = "0123456789"
upchars = "零一二三四五六七八九"
r=""
for x in s:
if x in lowchars:
r+= upchars
return r
num = input("请输入一个数字:")
print(foo(num)) smyiuo_11 发表于 2022-7-11 19:47
把列表换成字典确实方便了许多,
bigNum = ""
for i in j:
不难理解
j是传进来的数,因为他有好几个数字,所以需要用for循环遍历出每一个数字
在遍历出每一个数字后,就将这个数字放到字典中去查询,把查到的结果先放进bigNum里
页:
[1]