Python:重启后的变量保持值

时间:2015-07-21 14:36:53

标签: python function variables

我在python中创建了一个基本的Caesar Cypher,我已经完成了它的主要部分(cypher部分)但是由于我一直在测试代码,我发现我的shift变量保持相同的值当我使用函数重新启动代码时,我尝试重置所有变量,但它没有改变结果,这里是代码:

import sys
accept_yes = ["Yes", "yes", "Y", "y"]
accept_no = ["No", "no", "N", "n"]
accept_encrypt = ["Encrypt", "encrypt", "E", "e"]
accept_decrypt = ["Decrypt", "decrypt", "D", "d"]
accept_exit = ["Exit", "exit"]
norm_alph = ["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"]
crypt_alph = [""]

def Intro():
    encrypt_or_decrypt = input("Do you want to encrypt, decrypt or exit?: ") 
    if encrypt_or_decrypt in accept_encrypt:
        print("You chose encrypt")
        print("Enter a message to encrypt:  ") #Asks user for msg to encrypt
        Encrypt()
    elif encrypt_or_decrypt in accept_decrypt:
        print("You chose decrypt")
        print("Enter a message to decrypt:  ") #Asks user for msg to decrypt
        Decrypt()
    elif encrypt_or_decrypt in accept_exit:
        print ("Exiting...")
        sys.exit()
    else:
        print("Invalid Input")
        print("Please try again")
        Intro()

def Encrypt():
    en_or_de = ("")
    msg = input("").lower()
    crypt_msg = ("")
    shift = int(input("Enter a number to offset by\n"))
    shift = shift + 1
    for en_or_de in range(0,26):
        crypt_alph.append(norm_alph[(en_or_de+shift)%26])
    for en_or_de in msg:
        if norm_alph.count(en_or_de):
            crypt_msg += crypt_alph[norm_alph.index(en_or_de.lower())]
        else:
          crypt_msg += en_or_de
    print (crypt_msg.upper())
    Intro()

def Decrypt():
    en_or_de = ("")
    msg = input("").lower()
    crypt_msg = ("")
    shift = ("")
    shift = int(input("Enter a number to offset by\n"))
    shift = shift - 1
    for en_or_de in range(0,26):
        crypt_alph.append(norm_alph[(en_or_de-shift)%26])
    for en_or_de in msg:
      if norm_alph.count(en_or_de):
        crypt_msg += crypt_alph[norm_alph.index(en_or_de.lower())]
      else:
        crypt_msg += en_or_de
    print (crypt_msg.upper())
    Intro()

Intro()

我真的希望有人可以帮助我,因为我一直坚持这个问题。

提前谢谢!

1 个答案:

答案 0 :(得分:1)

您的问题是crypt_alph列表是在您第一次调用函数时设置的,然后每次调用Encrypt()函数时,新的crypt_alph内容都是从第一次通话中追加到crypt_alph

我添加了一行打印crypt_alph,以便您了解正在发生的事情:

Do you want to encrypt, decrypt or exit?: e
You chose encrypt
Enter a message to encrypt:  
Hello World
Enter a number to offset by
3
crypt_alph: ['', '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']
JGNNQ YQTNF
Do you want to encrypt, decrypt or exit?: e
You chose encrypt
Enter a message to encrypt:  
Hello World
Enter a number to offset by
4
crypt_alph: ['', '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', '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']
JGNNQ YQTNF
Do you want to encrypt, decrypt or exit?: exit
Exiting...

看看第二次crypt_alph打印的时间有多长?

为什么会发生这种情况的简短解释是python中的全局列表可以使用append()之类的操作进行更改。要解决您的问题,您应该将crypt_alph移到您的函数中,如下所示:

def Encrypt():
    crypt_alph = [""]
    en_or_de = ("")
    msg = input("").lower()
    crypt_msg = ("")
    shift = int(input("Enter a number to offset by\n"))
    shift = shift + 1
    for en_or_de in range(0,26):
        crypt_alph.append(norm_alph[(en_or_de+shift)%26])
    for en_or_de in msg:
        if norm_alph.count(en_or_de):
            crypt_msg += crypt_alph[norm_alph.index(en_or_de.lower())]
        else:
          crypt_msg += en_or_de
    print (crypt_msg.upper())
    Intro()

这是python中全局变量的一个棘手的小功能。一般来说,避免使用全局变量是个好主意!