2013-12-16 36 views
1

我最近開始嘗試學習python。作爲一個學習項目,我一直在製作一個小型加密/解密程序。我遇到的問題是我有一個while循環,所以程序可以從終端重新運行而不必退出並重新加載程序。然而,這會以某種方式連接'james'變量,以便每次運行都會添加到james變量中。在while循環中連接字符串的問題Python

當我所做的所有嘗試都失敗時,我該如何空白james變量?

#! /usr/bin/python 

import random 
import time 

#This program will encrypt and decrypt a string (line of text or numbers) 

#first we need a list of all the characters a-z, 0-9 and [email protected] 
Alphabet = [' ','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','0','!','"','£','$','%','^','&','*','(',')','@','?',',','\'','.',':' ] 
#53 objects in list 

#List to store each letter of the encrypted word 
encryptedWord = [] 



#Now to create a function to search for a letter and performs the encryption 
def encrypt(letter, Alphabet, encryptNo, operation): 

    index = Alphabet.index(letter)#Finds the index of the given letter 

    #These if statements determine whether the option for encryption or decryption 
    #has been chosen and then performs the operation 
    if operation == '1': #Encryption 
     if ((index + encryptNo)<=53): 
      newIndex = index + encryptNo 
     else: 
      newIndex = ((index + encryptNo)-53) 

    else:     #Decryption 
     if ((index - encryptNo)>=0): 
      newIndex = index - encryptNo 
     else: 
      newIndex = ((index - encryptNo)+53) 

    retLetter = Alphabet[newIndex] 
    return (retLetter) 

def displayIntro(): 
    print ('Welcome to the encryption and decryption tool') 
    print ('Encrypting some text translates the text into a secret code.') 
    print ('Decrypting a secret code will translate it back into normal text') 
    print() 
    print ('---------------------------------------------------------------------------') 
    print() 
    operation = input('Select from the options below.\n 1= Encryption\n 2= Decryption\n Choice: ') 

    encryptNo = int(input('Type your encryption Factor (1-25): ')) 

    wordToEncrypt = input('Type the word you would like to encrypt: ') 
    return (operation, encryptNo, wordToEncrypt) 


#now to the program, The playagain while loops is meant to allow for multiple running of the file. 
james = '' 
playAgain = 'yes' 
while playAgain == 'yes' or playAgain == 'y': 
    operation, encryptNo, wordToEncrypt = displayIntro() 

    #The next line splits the word string into seperate letters and fills a list called dave with each letter. 
    dave = [wordToEncrypt[i:i+1] for i in range(0, len(wordToEncrypt), 1)] 

    #Now I loop through the dave list sending the encrypt function each letter one at a time and returning the 
    #encrypted/decrypted letter 
    i=0 
    while i<=(len(dave)-1): 
     letter = dave[i] 
     encryptedLetter = encrypt(letter, Alphabet, encryptNo, operation) 
     encryptedWord.extend([encryptedLetter]) 
     i = i+1 

    #This is where My problem occurs. Each time I run through the while loop it is 
     #concatonating the james variable with what it was the previous run through. 
     #the del james doesnt seem to work either :(
    del james 
    james = ''.join(encryptedWord) 
    print() 
    if operation == '1': 
     print ('Your secret code is: ',james) 
    else: 
     print ('Your code said: ', james) 


    print() 
    print ('----------------------------------------------------------------------') 
    print() 
    print ('do you want to play again? (yes or no)') 
    playAgain = 'no' 
    playAgain = input() 

運行該程序使用加密因子10加密單詞john會返回單詞tyrx。如果選擇yes再次播放,然後解密tyrx,則返回tyrxjohn。

希望我明確自己。

+0

請查看http://docs.python.org/2/library/string.html#string.translate(和http://docs.python.org/2/library/string.html#string .maketrans)! – hop

回答

0

您需要在循環開始時重新初始化encryptedWord

encryptedWord = [] 
+0

感謝您的評論,我已經按照您的建議嘗試過了,但並未解決問題。 – TranquilityEden

+0

@ user3106771你說得對,我沒有意識到你對'james'的初始化(偶然是一個非常糟糕的變量名)是毫無意義的。同樣擺脫'del james'語句,因爲你在下一行重新創建'james'變量,所以它也沒有用處。 –

+0

AHHHHHH解決了它!謝謝!!! – TranquilityEden

1
  1. 你延長encryptedWord[]沒有重新初始化。
  2. 您未將james初始化爲在循環的開始或結束時爲空白。

解決方案:

初始化jamesecnryptedWord[]都爲空,在循環的開始。