鱼C论坛

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

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

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

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

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

x
  1. #/ -*- coding:utf-8 -*-
  2. s=input()
  3. i=0
  4. j=0
  5. len=len(s)

  6. str0=[0]*len
  7.         
  8. while(i<len):
  9.     if(s[i].isalpha()):
  10.             if(65<=ord(s[i]) and ord(s[i])<=77):
  11.                 temp1=ord(s[i])
  12.                 temp2=ord('Z')-(temp1-ord('A'))
  13.                 str0[i]=s[i].replace(s[i],str(temp2))
  14.                 str0[i]=chr(int(str0[i]))
  15.                 i+=1
  16.             elif(78<=ord(s[i]) and ord(s[i])<=90):
  17.                 temp1=ord(s[i])
  18.                 temp2=ord('A')+(ord('Z')-temp1)
  19.                 str0[i]=s[i].replace(s[i],str(temp2))
  20.                 str0[i]=chr(int(str0[i]))
  21.                 i+=1
  22.             elif(97<=ord(s[i]) and ord(s[i])<=109):
  23.                 temp1=ord(s[i])
  24.                 temp2=ord('z')-(temp1-ord('a'))
  25.                 str0[i]=s[i].replace(s[i],str(temp2))
  26.                 str0[i]=chr(int(str0[i]))
  27.                 i+=1
  28.             elif(110<=ord(s[i]) and ord(s[i])<=122):
  29.                 temp1=ord(s[i])
  30.                 temp2=ord('a')+(ord('z')-temp1)
  31.                 str0[i]=s[i].replace(s[i],str(temp2))
  32.                 str0[i]=chr(int(str0[i]))
  33.                 i+=1
  34.             else:
  35.                 i+=1
  36.     else:
  37.         str0[i]=s[i]
  38.         i+=1
  39. for test in str0:
  40.     print(test,end='')

复制代码



输入:
  1. 学好Python,用Coding建设祖国
复制代码

期望输出:中文保留,英文字符进行反码
实际输出:
  1. 00Kbgslm,0Xlwrmt0000
复制代码


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

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

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

以下是修正后的代码:

  1. s = input()
  2. str0 = []

  3. for ch in s:
  4.     if ch.isalpha():
  5.         if 'A' <= ch <= 'M':
  6.             temp1 = ord(ch)
  7.             temp2 = ord('Z') - (temp1 - ord('A'))
  8.             str0.append(chr(temp2))
  9.         elif 'N' <= ch <= 'Z':
  10.             temp1 = ord(ch)
  11.             temp2 = ord('A') + (ord('Z') - temp1)
  12.             str0.append(chr(temp2))
  13.         elif 'a' <= ch <= 'm':
  14.             temp1 = ord(ch)
  15.             temp2 = ord('z') - (temp1 - ord('a'))
  16.             str0.append(chr(temp2))
  17.         elif 'n' <= ch <= 'z':
  18.             temp1 = ord(ch)
  19.             temp2 = ord('a') + (ord('z') - temp1)
  20.             str0.append(chr(temp2))
  21.         else:
  22.             str0.append(ch)
  23.     else:
  24.         str0.append(ch)

  25. result = ''.join(str0)
  26. print(result)
复制代码


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

本帖被以下淘专辑推荐:

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

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

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

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

以下是修正后的代码:

  1. s = input()
  2. str0 = []

  3. for ch in s:
  4.     if ch.isalpha():
  5.         if 'A' <= ch <= 'M':
  6.             temp1 = ord(ch)
  7.             temp2 = ord('Z') - (temp1 - ord('A'))
  8.             str0.append(chr(temp2))
  9.         elif 'N' <= ch <= 'Z':
  10.             temp1 = ord(ch)
  11.             temp2 = ord('A') + (ord('Z') - temp1)
  12.             str0.append(chr(temp2))
  13.         elif 'a' <= ch <= 'm':
  14.             temp1 = ord(ch)
  15.             temp2 = ord('z') - (temp1 - ord('a'))
  16.             str0.append(chr(temp2))
  17.         elif 'n' <= ch <= 'z':
  18.             temp1 = ord(ch)
  19.             temp2 = ord('a') + (ord('z') - temp1)
  20.             str0.append(chr(temp2))
  21.         else:
  22.             str0.append(ch)
  23.     else:
  24.         str0.append(ch)

  25. result = ''.join(str0)
  26. print(result)
复制代码


这样修改后,输入 "学好Python,用Coding建设祖国",输出结果为 "学好Kbgslm,用Xlwrmt建设祖国",符合预期的结果。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

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

感谢!
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-4-22 18:09

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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