简体   繁体   English

如果我在程序中输入5却没有其他字母或数字,为什么我的程序将s和c错位?

[英]Why is my program shifting s and c by the wrong amount if I enter 5 into my program but not with any other letters or number?

This program is meant to ask you for a sentence and a number then it shifts the letters down the alphabet all by the inputted number and then lets you undo it by shift it by minus what you enter. 该程序旨在询问您一个句子和一个数字,然后将所有字母都按字母向下移动所输入的数字,然后通过将其减去输入的内容来将其撤消。 For some reason when you enter 5 as your shift the letter s shift to different random letters and does not give you the correct word when you try and shift back and I have no idea why. 出于某种原因,当您输入5作为班次时,字母s会转换为不同的随机字母,而当您尝试向后移时并不能为您提供正确的单词,我也不知道为什么。

import sys
import time
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"]
(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) = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
                                            15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26) 
def program():

    def encryption():

        def encryption1():
            global message
            global shift
            message = list ((input ("Please enter the sentence you would like to be %s\n>" % (EnDe1))).lower())
            print ("To %s your message please %s your private key number (from 1 - 10)" % (EnDe2, EnDe3))
            shift = int (input (">"))
            if EnDe == "b":
                shift = - (shift)
            if shift < 11 or shift > 0:
                for x in range(len(message)):        
                    if message[x] != " ":
                        if eval(message[x]) > 26 - shift:
                            message[x] = letters[eval(message[x]) + shift - 27]
                        else:
                            message[x] = letters[eval(message[x]) + shift - 1]
            else:
                shift = int (input ("only numbers from 1 to 10 are accepted, try again\n>"))
                encryption1()

        def choice():
            global EnDe
            global EnDe1
            global EnDe2
            global EnDe3
            EnDe = (input ("would you like to A)encrypt or B)decrypt\n>")).lower()
            if EnDe == "a":
                EnDe1 = "encrypted"
                EnDe2 = "encrypt"
                EnDe3 = "pick"
                encryption1()
            elif EnDe == "b":
                EnDe1 = "decrypted"
                EnDe2 = "decrypt"
                EnDe3 = "enter"
                encryption1()
            else:
                print ("please pick either 'A' or 'B' , ONLY!")
                time.sleep(2)
                choice()

        choice()              
        output = ''.join(message)
        print (output)
        retry = input ("would you like to Decrypt/Encrypt another message? (Y/N)\n>")
        retry = retry.lower()

        while retry != ("y" or "n"):
            retry = input ("please select either y or n\n>")
            retry = retry.lower()

        while retry == "y":
            program()
        else:
            sys.exit()
    encryption()     

The problem is that you define a global x variable, and also a local one. 问题是您定义了一个全局x变量,也定义了一个局部变量。 The local one shadows the global one and so the result of eval("x") is not anymore what you expected to have. 局部变量遮盖了全局变量,因此eval("x")的结果不再是您期望的。

Solution: use a different variable for the for loop. 解决方案:为for循环使用其他变量。

There is much that can be improved in your code. 您的代码有很多可以改进的地方。 You can take advantage of the modulo operator and the ord function, avoiding the need for all those 26 letter names. 您可以利用模运算符和ord函数,而无需使用所有这26个字母名称。

Here is how that for loop could look without all that: 没有所有这些,这就是for循环的样子:

if 0 < shift < 11:
    for i, ch in enumerate(message): 
        if ch != " ":
            message[i] = chr((ord(ch)-ord('a')+shift)%26+ord('a'))

Unrelated: note that retry != ("y" or "n") does not work like that. 无关:请注意, retry != ("y" or "n")不能那样工作。 You should do retry not in "yn" 您应该不要retry not in "yn"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM