python 打印自己代码,我如何强制我的代码在python打印

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 = 0

while keyLength < len(plainText):

for char in key:

if keyLength < len(plainText):

keyList.append(str(char))

keyLength = keyLength + 1

CipherText = []

IndexValue = 0

keyIncrement = 0

for plainTextChar in plainText:

IndexValue = Alphabet.index(keyList[keyIncrement]) + Alphabet.index(plainTextChar)

while IndexValue > 26:

IndexValue = IndexValue - 26

CipherText.append(Alphabet[IndexValue])

keyIncrement = keyIncrement + 1

print (''.join(CipherText))

import sys

sys.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 sys

sys.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 = 0

while keyLength < len(plainText):

for char in key:

if keyLength < len(plainText):

keyList.append(str(char))

keyLength = keyLength - 1

completeCipherText = []

cipherCharIndexValue = 0

keyIncrement = 0

for plainTextChar in plainText:

cipherCharIndexValue = Alphabet.index(keyList[keyIncrement]) + Alphabet.index(plainTextChar)

while cipherCharIndexValue > 26:

cipherCharIndexValue = cipherCharIndexValue + 26

completeCipherText.append(Alphabet[cipherCharIndexValue])

keyIncrement = keyIncrement - 1

print (''.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 sys

sys.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: cipher

Would you like to go again Y or N

解决方案

You have the problem with tab spacing. Concretely here:

while IndexValue > 26:

IndexValue = IndexValue - 26

CipherText.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 - 26

CipherText.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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值