在while循环Python期间出现字符串连接问题
我最近开始尝试学习python。作为一个学习项目,我一直在制作一个小型加密/解密程序。我的问题是,我有一个while循环在运行,因此程序可以从终端重新运行,而无需退出和重新加载程序。但是,这会以某种方式连接“james”变量,以便每次运行都会添加到james变量中 当我所做的所有尝试都失败时,如何将james变量留空在while循环Python期间出现字符串连接问题,python,python-3.x,Python,Python 3.x,我最近开始尝试学习python。作为一个学习项目,我一直在制作一个小型加密/解密程序。我的问题是,我有一个while循环在运行,因此程序可以从终端重新运行,而无需退出和重新加载程序。但是,这会以某种方式连接“james”变量,以便每次运行都会添加到james变量中 当我所做的所有尝试都失败时,如何将james变量留空 #! /usr/bin/python import random import time #This program will encrypt and decrypt a st
#! /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 !-@
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。如果选择“是”再次播放,然后解密tyrx,则返回tyrxjohn
希望我能说清楚。您需要在循环开始时重新初始化encryptedWord
encryptedWord = []
您正在扩展encryptedWord[],而无需重新初始化它。
您没有在循环的开始或结束时将james初始化为blank。
解决方案:
在循环开始时,将james和ecnryptedWord[]都初始化为空。谢谢您的评论,我已经按照您的建议尝试了,但没有解决问题。@user3106771您是对的,我没有意识到您对james的初始化(顺便说一句,它是一个非常糟糕的变量名)是毫无意义的。同时去掉del james语句,它也没有任何用处,因为您在下一行重新创建了james变量。请查看和!
encryptedWord = []