有人能帮我解决这个问题吗

2024-09-26 18:10:59 发布

您现在位置:Python中文网/ 问答频道 /正文

我的任务是加密和解密编码,并创建一个按钮或一个字,将退出程序。现在,我已经想出了如何做到这一点,但现在我必须使用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()

Tags: thetoinposyouinputifascii
1条回答
网友
1楼 · 发布于 2024-09-26 18:10:59

这是你的代码的修复版本。正确的缩进在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

相关问题 更多 >

    热门问题