鱼C论坛

 找回密码
 立即注册
查看: 1429|回复: 2

[已解决]Python字符串反码.2

[复制链接]
发表于 2023-9-25 18:06:08 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
#/ -*- coding:utf-8 -*-
s=input()
i=0
j=0
len=len(s)

str0=[0]*len
        
while(i<len):
    if(s[i].isalpha()):
            if(65<=ord(s[i]) and ord(s[i])<=77):
                temp1=ord(s[i])
                temp2=ord('Z')-(temp1-ord('A'))
                str0[i]=s[i].replace(s[i],str(temp2))
                str0[i]=chr(int(str0[i]))
                i+=1
            elif(78<=ord(s[i]) and ord(s[i])<=90):
                temp1=ord(s[i])
                temp2=ord('A')+(ord('Z')-temp1)
                str0[i]=s[i].replace(s[i],str(temp2))
                str0[i]=chr(int(str0[i]))
                i+=1
            elif(97<=ord(s[i]) and ord(s[i])<=109):
                temp1=ord(s[i])
                temp2=ord('z')-(temp1-ord('a'))
                str0[i]=s[i].replace(s[i],str(temp2))
                str0[i]=chr(int(str0[i]))
                i+=1
            elif(110<=ord(s[i]) and ord(s[i])<=122):
                temp1=ord(s[i])
                temp2=ord('a')+(ord('z')-temp1)
                str0[i]=s[i].replace(s[i],str(temp2))
                str0[i]=chr(int(str0[i]))
                i+=1
            else:
                i+=1
    else:
        str0[i]=s[i] 
        i+=1
for test in str0:
    print(test,end='')


输入:
学好Python,用Coding建设祖国
期望输出:中文保留,英文字符进行反码
实际输出:
00Kbgslm,0Xlwrmt0000

求大佬解答其中原因!!!
最佳答案
2023-9-25 18:06:32
这段代码的目的是将输入的字符串中的英文字符进行反码处理,保留中文字符不变。

问题出在对于英文字符的反码处理上。代码中使用了replace()方法来替换字符,然后再将替换后的字符串转换为整数,最后再转换为字符。这样的处理方式是不正确的。

正确的处理方式是使用ord()函数获取字符的ASCII码,然后根据ASCII码进行反码处理,最后再将反码转换为字符。

以下是修正后的代码:
s = input()
str0 = []

for ch in s:
    if ch.isalpha():
        if 'A' <= ch <= 'M':
            temp1 = ord(ch)
            temp2 = ord('Z') - (temp1 - ord('A'))
            str0.append(chr(temp2))
        elif 'N' <= ch <= 'Z':
            temp1 = ord(ch)
            temp2 = ord('A') + (ord('Z') - temp1)
            str0.append(chr(temp2))
        elif 'a' <= ch <= 'm':
            temp1 = ord(ch)
            temp2 = ord('z') - (temp1 - ord('a'))
            str0.append(chr(temp2))
        elif 'n' <= ch <= 'z':
            temp1 = ord(ch)
            temp2 = ord('a') + (ord('z') - temp1)
            str0.append(chr(temp2))
        else:
            str0.append(ch)
    else:
        str0.append(ch)

result = ''.join(str0)
print(result)

这样修改后,输入 "学好Python,用Coding建设祖国",输出结果为 "学好Kbgslm,用Xlwrmt建设祖国",符合预期的结果。

本帖被以下淘专辑推荐:

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2023-9-25 18:06:32 | 显示全部楼层    本楼为最佳答案   
这段代码的目的是将输入的字符串中的英文字符进行反码处理,保留中文字符不变。

问题出在对于英文字符的反码处理上。代码中使用了replace()方法来替换字符,然后再将替换后的字符串转换为整数,最后再转换为字符。这样的处理方式是不正确的。

正确的处理方式是使用ord()函数获取字符的ASCII码,然后根据ASCII码进行反码处理,最后再将反码转换为字符。

以下是修正后的代码:
s = input()
str0 = []

for ch in s:
    if ch.isalpha():
        if 'A' <= ch <= 'M':
            temp1 = ord(ch)
            temp2 = ord('Z') - (temp1 - ord('A'))
            str0.append(chr(temp2))
        elif 'N' <= ch <= 'Z':
            temp1 = ord(ch)
            temp2 = ord('A') + (ord('Z') - temp1)
            str0.append(chr(temp2))
        elif 'a' <= ch <= 'm':
            temp1 = ord(ch)
            temp2 = ord('z') - (temp1 - ord('a'))
            str0.append(chr(temp2))
        elif 'n' <= ch <= 'z':
            temp1 = ord(ch)
            temp2 = ord('a') + (ord('z') - temp1)
            str0.append(chr(temp2))
        else:
            str0.append(ch)
    else:
        str0.append(ch)

result = ''.join(str0)
print(result)

这样修改后,输入 "学好Python,用Coding建设祖国",输出结果为 "学好Kbgslm,用Xlwrmt建设祖国",符合预期的结果。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2023-9-25 18:10:14 | 显示全部楼层
isdkz 发表于 2023-9-25 18:06
这段代码的目的是将输入的字符串中的英文字符进行反码处理,保留中文字符不变。

问题出在对于英文字符的 ...

感谢!
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-9-21 10:59

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表