我的任务是加密和解密编码,并创建一个按钮或一个字,将退出程序。现在,我已经想出了如何做到这一点,但现在我必须使用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()
这是你的代码的修复版本。正确的缩进在Python中非常重要,因为这是将代码分组成块的方式。如果代码缩进是错误的,Python解释器和阅读程序的人都不能告诉您希望它做什么。你知道吗
我们可以将
letters
简化为单个字符串,而不是字符串的元组。你知道吗我所做的主要更改是将不在
letters
中的字符复制到newPhrase
,而不移动它们。这意味着空格和标点符号会被复制,但也意味着数字和大写字母也会被复制。你知道吗我还将您的程序放入一个大循环中,这样
"Do you want to exit? "
的内容就有意义了。否则,程序将退出。你知道吗测试输出
相关问题 更多 >
编程相关推荐