又用到了string这个模块,ascii字符在这里被归纳的很好
字符变换分为多个类型: 使用独立的change_letter函数进行字符变换
- 大写字母变为向后移动1位的小写字母,如果是Z则为a, a则为b
- 数字及字符则不变
- 小写字母按实际转换为手机九宫格数字
主函数遍历多组输入: 对单个输入遍历并改变字符后放入新数组,最后打印
import string
import sys
def change_letter(letter):
if letter == 'Z':
return 'a'
if letter in string.ascii_uppercase:
index = string.ascii_uppercase.index(letter) + 1
return string.ascii_lowercase[index]
if letter in string.digits + string.punctuation:
return letter
if letter in 'abc':
return '2'
elif letter in 'def':
return '3'
elif letter in 'ghi':
return '4'
elif letter in 'jkl':
return '5'
elif letter in 'mno':
return '6'
elif letter in 'pqrs':
return '7'
elif letter in 'tuv':
return '8'
elif letter in 'wxyz':
return '9'
if __name__ == '__main__':
data = sys.stdin
for x in data:
old_password = x.strip()
new_password = []
for i in range(len(old_password)):
value = change_letter(old_password[i])
new_password.append(value)
print(''.join(new_password))