我的任务是加密和解密编码,还创建一个退出程序的按钮或单词。现在,我已经研究了如何做到这一点,但现在我必须使用ASCII,我需要一些帮助...这是我的代码,只需加密,解密和退出下面。任何人都可以帮我解决ASCII部分...
welcome = input("Hello there...")
letters = ("a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z")
numberShift = int(input("How much do you wish to shift the letters? "))
phrase = input("Enter a phrase to be encrypted: ")
newPhrase = ""
for l in phrase:
if l in letters:
pos = letters.index(l) + numberShift
if pos > 25:
pos = pos - 26
newPhrase += letters[pos]
else:
newPhrase += " "
print(newPhrase)
numberShift = int(input("How many times has the code you want to decrypt been shifted? "))
phrase = input("Enter a phrase to be decrypted: ")
newPhrase = ""
for l in phrase:
if l in letters:
pos = letters.index(l) - numberShift
if pos > 25:
pos = pos - 26
newPhrase += letters[pos]
else:
newPhrase += " "
print(newPhrase)
exit1 = input("Do you want to exit?")
if 'yes' in exit1:
exit()
答案 0 :(得分:1)
这是修复后的代码版本。在Python中,正确的缩进非常很重要,因为这是将代码分组为块的方式。如果代码缩进错误,那么Python解释器和阅读程序的人都无法告诉你想要它做什么。
我们可以将letters
简化为单个字符串,而不是字符串元组。
我所做的主要更改是将letters
以内的字符复制到newPhrase
而不移动它们。这意味着空格和标点符号会被复制,但这也意味着数字和大写字母也会被复制。
我还将你的程序放到一个大循环中,这样"Do you want to exit? "
的东西才有意义。否则,程序将会退出。
letters = "abcdefghijklmnopqrstuvwxyz"
welcome = print("Hello there!")
while True:
#Encrypt
numberShift = int(input("How much do you wish to shift the letters? "))
phrase = input("Enter a phrase to be encrypted: ")
newPhrase = ""
for l in phrase:
#Only change characters that aren't in letters
if l in letters:
pos = letters.index(l) + numberShift
if pos > 25:
pos = pos - 26
l = letters[pos]
newPhrase += l
print(newPhrase)
#Decrypt
numberShift = int(input("How many times has the code you want to decrypt been shifted? "))
phrase = input("Enter a phrase to be decrypted: ")
newPhrase = ""
for l in phrase:
#Only change characters that aren't in letters
if l in letters:
pos = letters.index(l) - numberShift
if pos > 25:
pos = pos - 26
l = letters[pos]
newPhrase += l
print(newPhrase)
if 'yes' in input("Do you want to exit? "):
break
测试输出
Hello there!
How much do you wish to shift the letters? 3
Enter a phrase to be encrypted: somewhere in la mancha, in a place whose name I do not care to remember, a gentleman lived not long ago, one of those who has a lance and ancient shield on a shelf and keeps a skinny nag and a greyhound for racing.
vrphzkhuh lq od pdqfkd, lq d sodfh zkrvh qdph I gr qrw fduh wr uhphpehu, d jhqwohpdq olyhg qrw orqj djr, rqh ri wkrvh zkr kdv d odqfh dqg dqflhqw vklhog rq d vkhoi dqg nhhsv d vnlqqb qdj dqg d juhbkrxqg iru udflqj.
How many times has the code you want to decrypt been shifted? 3
Enter a phrase to be decrypted: vrphzkhuh lq od pdqfkd, lq d sodfh zkrvh qdph I gr qrw fduh wr uhphpehu, d jhqwohpdq olyhg qrw orqj djr, rqh ri wkrvh zkr kdv d odqfh dqg dqflhqw vklhog rq d vkhoi dqg nhhsv d vnlqqb qdj dqg d juhbkrxqg iru udflqj.
somewhere in la mancha, in a place whose name I do not care to remember, a gentleman lived not long ago, one of those who has a lance and ancient shield on a shelf and keeps a skinny nag and a greyhound for racing.
Do you want to exit? yes