大佬教程收集整理的这篇文章主要介绍了为什么这个python代码一次打印一个字母?,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
def encrypt():
msg = input("Enter the message you would like to encrypt: ").strip()
print()
key = int(input("Enter key to encrypt,a number 0-25: "))
encryptedMessage = ""
for ch in msg:
if ord(ch) == 32:
encryptedMessage += ch
elif ord(ch) + key > 122:
#after z moves back to a,a = 97,z = 122
temp = (ord(ch) + key) - 122
encryptedMessage += chr(96+temp)
elif (ord(ch) + key > 90) and (ord(ch) < 96):
#moving back to a after z
temp = (ord(ch) + key) - 90
encryptedMessage += chr(64+temp)
else:
#incase of letters being both caps
encryptedMessage += chr(ord(ch) + key)
print(encryptedMessage)
main()
很抱歉有大量代码,我真的不知道是什么导致了这种情况。这应该是一个 Caesar Cipher 加密程序,你输入消息并移动,它应该打印代码。然而,它只打印一个字母,如果我在最后删除 main(),它会一次打印每个字母,例如让我们说这个词是 Lemons,它是 L、LE、LEM 等等。如果有人知道如何帮助解决这个问题,我将不胜感激,谢谢!
只需取消缩进 print(encryptedMessage)
这样它就不会在循环中。
def encrypt():
...
for ch in msg:
...
print(encryptedMessage)
,
在这里,这应该适合您的需求
def encrypt():
msg = input("Enter the message you would like to encrypt: ").strip()
print()
key = int(input("Enter key to encrypt,a number 0-25: "))
encryptedMessage = ""
for ch in msg:
if ord(ch) == 32:
encryptedMessage += ch
elif ord(ch) + key > 122:
#after z moves back to a,a = 97,z = 122
temp = (ord(ch) + key) - 122
encryptedMessage += chr(96+temp)
elif (ord(ch) + key > 90) and (ord(ch) < 96):
#moving back to a after z
temp = (ord(ch) + key) - 90
encryptedMessage += chr(64+temp)
else:
#incase of letters being both caps
encryptedMessage += chr(ord(ch) + key)
print(encryptedMessage)
encrypt()
现在,如果你看到了,它运行得很好
Enter the message you would like to encrypt: Hello World
Enter key to encrypt,a number 0-25: 15
Wtaad Ldgas
Process finished with exit code 0
以上是大佬教程为你收集整理的为什么这个python代码一次打印一个字母?全部内容,希望文章能够帮你解决为什么这个python代码一次打印一个字母?所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。