【发布时间】:2013-12-16 09:47:38
【问题描述】:
我正在尝试使用以下代码将拒绝字符串转换为字符,但无法正确转换它们。
# Set to true if testing. Will show lists that show the process.
testing = True
# Asks for the denary.
text = list(input('Enter the denary: '))
# Defining the list. 0 is there to allow the remover to work correctly.
doublesList = ['0']
# Split the numbers into doubles and append them to doublesList.
for i in range(len(text) - 1):
current_item = text[i]
next_item = text[i + 1]
doubles = current_item + next_item
doublesList.append(doubles)
if testing:
print('\nunedited:\n', doublesList,'\n')
# Remove unnecessary numbers.
for item in doublesList:
doublesList.remove(item)
if testing:
print('edited:\n', doublesList,'\n')
# Replace the numbers with their letters.
for item in doublesList:
if item == '01':
doublesList[doublesList.index('01')] = 'a'
elif item == '02':
doublesList[doublesList.index('02')] = 'b'
elif item == '03':
doublesList[doublesList.index('03')] = 'c'
elif item == '04':
doublesList[doublesList.index('04')] = 'd'
elif item == '05':
doublesList[doublesList.index('05')] = 'e'
elif item == '06':
doublesList[doublesList.index('06')] = 'f'
elif item == '07':
doublesList[doublesList.index('07')] = 'g'
elif item == '08':
doublesList[doublesList.index('08')] = 'h'
elif item == '09':
doublesList[doublesList.index('09')] = 'i'
elif item == '10':
doublesList[doublesList.index('10')] = 'j'
elif item == '11':
doublesList[doublesList.index('11')] = 'k'
elif item == '12':
doublesList[doublesList.index('12')] = 'l'
elif item == '13':
doublesList[doublesList.index('13')] = 'm'
elif item == '14':
doublesList[doublesList.index('14')] = 'n'
elif item == '15':
doublesList[doublesList.index('15')] = 'o'
elif item == '16':
doublesList[doublesList.index('16')] = 'p'
elif item == '17':
doublesList[doublesList.index('17')] = 'q'
elif item == '18':
doublesList[doublesList.index('18')] = 'r'
elif item == '19':
doublesList[doublesList.index('19')] = 's'
elif item == '20':
doublesList[doublesList.index('20')] = 't'
elif item == '21':
doublesList[doublesList.index('21')] = 'u'
elif item == '22':
doublesList[doublesList.index('22')] = 'v'
elif item == '23':
doublesList[doublesList.index('23')] = 'w'
elif item == '24':
doublesList[doublesList.index('24')] = 'x'
elif item == '25':
doublesList[doublesList.index('25')] = 'y'
elif item == '26':
doublesList[doublesList.index('26')] = 'z'
elif item == '27':
doublesList[doublesList.index('27')] = ' '
elif item == '28':
doublesList[doublesList.index('28')] = '.'
elif item == '29':
doublesList[doublesList.index('29')] = ''
elif item == '30':
doublesList[doublesList.index('30')] = '\n'
# Print the finished list as a string.
print(''.join(doublesList))
使用上述代码,输入拒绝号码“2908051212152830132527140113052709192702150228”应返回“hello.\nmy name is bob.”。但是,如您所见,它返回“hello.my nam\ne is bob.”:
Enter the denary: 2908051212152830132527140113052709192702150228
unedited:
['0', '29', '90', '08', '80', '05', '51', '12', '21', '12', '21', '15', '52', '28', '83', '30', '01', '13', '32', '25', '52', '27', '71', '14', '40', '01', '11', '13', '30', '05', '52', '27', '70', '09', '91', '19', '92', '27', '70', '02', '21', '15', '50', '02', '22', '28']
edited:
['29', '08', '05', '12', '12', '15', '28', '13', '25', '27', '14', '01', '13', '30', '05', '27', '09', '19', '27', '02', '15', '02', '28']
hello.my nam
e is bob.
谁能提出一个可能的解决办法?谢谢。 为大量代码道歉。
这不是重复的...
【问题讨论】:
-
“拒绝”是一个官方术语吗?
-
@MartijnPieters:Denary 是一个不太常见的十进制术语
-
@AswinMurugesh:原来如此;在我的经验中相当罕见,但我也不是以英语为母语的人。
-
@MartijnPieters 它对我来说看起来不像是重复的......这与我正在寻找的完全不同。
-
我是以英语为母语的人,我不认为“denary”是一个常见的术语——如果不检查谷歌我也不知道它是什么意思。并不是说一旦人们弄清楚它在这种情况下的含义就真的很重要,但这肯定不是我方言的一部分。