Answer to Question #195303 in Python for mariaaaaaa

Question #195303

What is wrong with his code? It is not running, please check it.

def encrypt(message,shift): 

  encrypted_message = " "

lst=[]

lst.extend(message)

for x in lst: 

if(x.isalpha()): 

if(ord(x)<= 90 and (ord(x)+shift) > 90):

 new =(ord(x)+shift)- 90

encrypted_message += chr(64+new)

elif(ord(x)<= 122 and (ord(x)+shift) > 122):  

new =(ord(x)+shift)- 122

 encrypted_message += chr(96+new)

  else: 

   encrypted_message += chr(ord(x) + shift)

  else: 

 encrypted_message+=x

print(encrypted_message)

def main(): 

message=input() 

 shift=int(input())

while(shift<0 or shift>25):

print("Invalid shift")

shift=int(input())

encrypt(message,shift)

main() 



1
Expert's answer
2021-05-19T01:37:34-0400
def encrypt(message,shift):


	encrypted_message = ""
	for x in message:
		if(x.isalpha()):
			if(ord(x) <= 90 and (ord(x) + shift) > 90):
				new = (ord(x) + shift) - 90
				encrypted_message += chr(64 + new)
			elif(ord(x) <= 122 and (ord(x) + shift) > 122):
				new =(ord(x) + shift) - 122
				encrypted_message += chr(96 + new)
			else:
				encrypted_message += chr(ord(x) + shift)
		else:
			encrypted_message += x
	print(encrypted_message)


def main():
	
	message = input() 
	shift = int(input())
	while(shift < 0 or shift > 25):
		print("Invalid shift")
		shift = int(input())
	encrypt(message, shift)


main()

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS