2 回答

TA贡献1810条经验 获得超4个赞
通过使用处理第一个条件中的所有非字母字符,您只需更改一个班轮即可获得所需的结果 isalpha()
# encryption
message = input("Enter a message to be encrypted: ") # user inputs message to be encrypted
offset = int(input ("Enter an offset: ")) # user inputs offset
print ("\t")
encrypt = " "
for char in message:
if not char.isalpha(): #changed
encrypt = encrypt + char
elif char.isupper():
encrypt = encrypt + chr((ord(char) + offset - 65) % 26 + 65) # for uppercase Z
else:
encrypt = encrypt + chr((ord(char) + offset - 97) % 26 + 97) # for lowercase z
print ("Your original message:",message)
print ("Your encrypted message:",encrypt)
print ("\t")

TA贡献1803条经验 获得超3个赞
就像你用空格做的一样,你可以用任何字符来做。
if char in string.punctuation+' ': encrypt = encrypt + char
添加回答
举报