How do i force my code to print in python

2024/9/22 12:37:31

I'm having trouble trying to work out an error in my code. It isn't printing the final product and leaving a blank space.

playing = True
string = ""
Alphabet = ('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')while playing == True:string = ""eord = input('Type "d" to "decrypt" and "e" to "encrypt": ')if eord == 'e':texte = input ("Type your word to encrypt: ")key1 = int(input("Choose a key between 1-26: "))for letter in texte:number = (ord(letter)) + (key1)letter=(chr(number))string = (str(string)) + (str(letter))print (string)keyword = input ("Type 'encrypt' code further or 'decrypt' further: ")if keyword == 'encrypt':plainText = input("Please enter the plain text: ")key = input("Please enter the key: ")keyList = []keyLength = 0while keyLength < len(plainText):for char in key:if keyLength < len(plainText):keyList.append(str(char))keyLength = keyLength + 1CipherText = [] IndexValue = 0keyIncrement = 0for plainTextChar in plainText:IndexValue = Alphabet.index(keyList[keyIncrement]) + Alphabet.index(plainTextChar)while IndexValue > 26:IndexValue = IndexValue - 26CipherText.append(Alphabet[IndexValue])keyIncrement = keyIncrement + 1print (''.join(CipherText))import syssys.stdout.flush()finish = input('Would you like to go again Y or N')if finish == 'n' or 'N':retry = input ("Would you like to go again? Y or N: ")if retry == 'N' or 'n':print ("Please exit the window")import syssys.exit()elif eord == 'd':texd = input ("Type your word to decrypt: ")key2 = int(input("Choose a key between 1-16: "))for letter in texd:number = (ord(letter)) - (key2)letter=(chr(number))string = (str(string)) + (str(letter))print (string)keyword = input ("Type 'encrypt' code further or 'decrypt' further: ")if keyword == 'decrypt':plainText = input("Please enter the plain text: ")key = input("Please enter the key: ")keyList = []keyLength = 0while keyLength < len(plainText):for char in key:if keyLength < len(plainText):keyList.append(str(char))keyLength = keyLength - 1completeCipherText = [] cipherCharIndexValue = 0keyIncrement = 0for plainTextChar in plainText:cipherCharIndexValue = Alphabet.index(keyList[keyIncrement]) + Alphabet.index(plainTextChar)while cipherCharIndexValue > 26:cipherCharIndexValue = cipherCharIndexValue + 26completeCipherText.append(Alphabet[cipherCharIndexValue])keyIncrement = keyIncrement - 1print (''.join(completeCipherText))finish = input('Would you like to go again Y or N')if finish == 'n' or 'N':retry = input ("Would you like to go again? Y or N: ")if retry == 'N' or 'n':print ("Please exit the window")import syssys.exit()

Is there a way i can fix this or force it to print?

Here is the output of the code.

Type "d" to "decrypt" and "e" to "encrypt": e
Type your word to encrypt: hello
Choose a key between 1-26: 3
khoor
Type 'encrypt' code further or 'decrypt' further: encrypt
Please enter the plain text: python
Please enter the key: cipherWould you like to go again Y or N
Answer

You have the problem with tab spacing. Concretely here:

                     while IndexValue > 26:IndexValue = IndexValue - 26CipherText.append(Alphabet[IndexValue])keyIncrement = keyIncrement + 1

For the first pass the IndexValue is 19, so it skips also CipherText append. And therefore there is nothing to print out.

Correct indentation here:

                     while IndexValue > 26:IndexValue = IndexValue - 26CipherText.append(Alphabet[IndexValue])keyIncrement = keyIncrement + 1

Then again you are asking whether to finish when still in the encrypting for loop.

However you have other flaws in your design. If the key is longer than 1 char you end up in Index error due to the keyList having just just 1 item. While you expect more items in keyList in the for loop.

https://en.xdnf.cn/q/119130.html

Related Q&A

Adding specific days in python table

I have a dataset (Product_ID,date_time, Sold) which has products sold on various dates. The dates are not consistent and are given for 9 months with random 13 days or more from a month. I have to segre…

django how to following relationships backwards?

I am having some issue with following relationships backwards. From the parent page i want to be able to see what children belong to that parent. Heres what i got so farmodel.pyclass Parents(models.Mod…

Python File handling: Seaching for specific numbers

Im creating a document in which I need to record license plates of vehicles (its a practice exercise, nothing illegal) and calculate the speed they travel at and display all the vehicles that are trave…

How to convert token list into wordnet lemma list using nltk?

I have a list of tokens extracted out of a pdf source. I am able to pre process the text and tokenize it but I want to loop through the tokens and convert each token in the list to its lemma in the wor…

Script throws an error when it is made to run using multiprocessing

Ive written a script in python in combination with BeautifulSoup to extract the title of books which get populated upon providing some ISBN numbers in amazon search box. Im providing those ISBN numbers…

Efficiently pair random elements of list

I have a list of n elements say: foo = [a, b, c, d, e] I would like to randomly pair elements of this list to receive for example: bar = [[a, c], [b, e]] where the last element will be discarded if the…

ALL permutations of a list with repetition but not doubles

I have seen similar but not the same: here. I definitely want the permutations, not combinations, of all list elements. Mine is different because itertools permutation of a,b,c returns abc but not aba …

NameError: name current_portfolio is not defined

I am getting NameError: name current_portfolio is not defineddef initialize(context): context.sym = symbol(xxx) context.i = 0def handle_data(context, data):context.i += 1 if context.i < 60:returnsma…

Scrape an Ajax form with .submit() with Python and Selenium

I am trying to get the link from a web page. The web page sends the request using javascript, then the server sends a response which goes directly to download a PDF. This new PDF is automatically downl…

How to process break an array in Python?

I would like to use a double array. But I still fail to do it. This what I did. Folder = "D:\folder" Name = [gadfg5, 546sfdgh] Ver = [None, hhdt5463]for dn in Name :for dr in Ver :if dr is No…