问题描述
我想知道这个代码的合适的循环,在它到达Z之后将它带回到A,我对python非常陌生,这对我有很大帮助,谢谢。
reset =yes
def encrypt():
answer =
for message in message :
new = ord(letter)+ offset
answer = answer + chr(new)
print(answer)
def解密():
answer =
用于信件中的信件:
new = ord(字母) - offset
answer = answer + chr(新)
print (答案)
while reset ==yes:
message = input(输入消息)
offset = int(input( ))
reset =yes
while offset> 26:
offset = int(input(你要多少个字母来抵消?(0-26)))
eord = input(输入E用于加密,或者D)解密)
如果eord ==e:
加密()
elif eord ==d:
解密()
else:
eord = input(类型E用于加密,或者D用于解密)
reset = input(你想要做另一个吗?)
$ c $正如我在评论中提到的,你要求'E'或'D',但是检查'e'或'd'。在最后一行,你应该告诉他们输入'yes'(而不是'Yes'或'Y')。您还需要将消息和偏移变量传递给加密和解密函数。
您提到它应该从Z重新回到A(而不是使用非字母表轮班中的人物)。为此,您需要使用 if
语句来测试 ord
值,如:
用于信件中的信件:
#..删除了一些代码..
#这部分特别是你想要的(对于A和a)
elif ord(letter)== 122: #'z'的ord值为122
new ='a'
#等等...
以下是您的代码的编辑版本。它只需要在 encrypt
和解密的定义中提到的 ord
/ code>函数:
reset =yes
def encrypt(message,抵消):
answer =
用于信件中的信件:
new = ord(信件)+抵消
answer = answer + chr(新)
print(答案)
def解密(消息,抵消):
answer =
用于信件中的信件:
new = ord(信件) - 抵消
答案=答案+ chr(新)
打印(答案)
while reset ==yes:
message = input(输入消息。)
offset = int(input(你想用多少字母来抵消这些字母?))
while offset> 26:
offset = int(input(你要多少个字母来抵消?(0-26)))
eord = input(输入E用于加密,或者D'解密)
while eord!='E'or e!!='D':
如果eord ==E:
加密(消息,偏移)
elif eord ==D:
decrypt(message,offset)
reset = input(你想做另一个(键入'yes')?)
I want to know a suitable loop for this code, to take it back to A after it reaches Z, i am very new to python and this would help me out a lot, thanks.
reset = "yes"
def encrypt():
answer = ""
for letter in message:
new=ord(letter)+ offset
answer=answer+chr(new)
print(answer)
def decrypt():
answer = ""
for letter in message:
new=ord(letter)- offset
answer=answer+chr(new)
print(answer)
while reset == "yes":
message= input("Type in a message.")
offset= int(input("How many do you want offset the letters by?"))
reset = "yes"
while offset > 26:
offset= int(input("How many do you want offset the letters by?(0-26)"))
eord=input("Type E for encrypt, or D for decrypt")
if eord == "e":
encrypt()
elif eord == "d":
decrypt()
else:
eord=input("Type E for encrypt, or D for decrypt")
reset= input("do you want to do another?")
解决方案 As I mentioned in comments, you are asking for 'E' or 'D', but checking for 'e' or 'd'. On your last line you should probably tell them to type 'yes' (rather than 'Yes' or 'Y'). Also you need to pass the message and offset variables to the encypt and decrypt functions.
You mentioned that it should recycle back to A from Z (rather than using non-alphabet characters in the shift). For that, you want to test the ord
value using if
statements, like this example:
for letter in message:
# ..removed some code..
# This part in particular is what you want (for both A and a)
elif ord(letter) == 122: # 'z' has ord value of 122
new = 'a'
# and so on ...
Here is an edited version of your code. All it needs is the ord
check mentioned above in your definitions of the encrypt
and decrypt
functions:
reset = "yes"
def encrypt(message, offset):
answer = ""
for letter in message:
new=ord(letter)+ offset
answer=answer+chr(new)
print(answer)
def decrypt(message, offset):
answer = ""
for letter in message:
new=ord(letter)- offset
answer=answer+chr(new)
print(answer)
while reset == "yes":
message= input("Type in a message.")
offset= int(input("How many do you want offset the letters by?"))
while offset > 26:
offset= int(input("How many do you want offset the letters by?(0-26)"))
eord=input("Type E for encrypt, or D for decrypt")
while eord != 'E' or eord != 'D':
if eord == "E":
encrypt(message, offset)
elif eord == "D":
decrypt(message, offset)
reset= input("do you want to do another (type 'yes')?")
这篇关于凯撒密码循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!