Python密码解密函数输出不符合预期,寻求解决方案
Python密码解密函数输出不符合预期问题
我用Python实现了一个密码解密函数,要求根据字符索引交替应用不同旋转值,并且给元音字符前后用0包裹,但实际输出和预期结果不符。
我的代码
def decrypt_password(password: str) -> str: rot7, rot9 = 7, 9 # Rotation values vowels = 'AEIOUaeiou' # Vowels to check decrypted = [] # List to hold decrypted characters for index, char in enumerate(password): if char in vowels: rotation_key = rot9 if index % 2 == 1 else rot7 # Use 9 for odd indices decrypted.append('0') # Add '0' before the vowel else: rotation_key = rot7 if index % 2 == 0 else rot9 # Use 7 for even indices if 33 <= ord(char) <= 126: # Check if character is within the allowed ASCII range decrypted_char = chr((ord(char) - rotation_key - 33) % 94 + 33) # Wrap around decrypted.append(decrypted_char) # Add decrypted character to the list else: decrypted.append(char) # Append unchanged if out of range if char in vowels: decrypted.append('0') # Add '0' after the vowel return ''.join(decrypted) # Join the list into a string and return
测试用例
# Test case print(decrypt_password("bAnanASplit"))
预期输出
"i0J0u0j0u0J0Zys0r0{"
实际输出
"0b0'C'0n0'c'0n0'C'Spl'k't"
我已经添加了打印语句跟踪字符转换过程,但仍然得到错误结果,使用环境为Python 3.12。
内容的提问来源于stack exchange,提问作者miali2
相关产品推荐
相关产品推荐

