Rachel M.

asked • 02/20/23

Python 3 homework string help

This si what I'm supposed to do.

Define a function named encrypt(msg) that takes a string message as the argument, and returns the message in an "encrypted" form using the encryption rules described below:

  1. If the character is an uppercase: change it to lowercase with '!' sign on its both sides (e.g. 'M' --> '!m!');
  2. If the character is a lowercase: change it to uppercase with '!' sign on its both sides (e.g. 'm' --> '!M!');
  3. If the character is a digit: cube the digit, i.e. raise to the power of 3 (e.g. '5' --> '125');
  4. If the character is a blank space: replace it with '$' symbol;
  5. Otherwise: keep it as it is.

Examples:

encrypt('500 Miles') returns '12500$!m!!I!!L!!E!!S!'

encrypt('Tesla Model 3.') returns '!t!!E!!S!!L!!A!$!m!!O!!D!!E!!L!$27.'

encrypt('go-2-sky') returns '!G!!O!-8-!S!!K!!Y!'


this is my current code

def encrypt(msg):

  encrypted_msg = " "

  for str in msg:

    if str.isupper():

      encrypted_msg += "!" + str.lower() + "!"

    elif str.islower():

      encrypted_msg += "!" + str.upper() + "!"

    elif str.isdigit():

      encrypted_msg += str(int(str) ** 3)

    elif str.isspace():

      encrypted_msg += "$"

    else:

      encrypted_msg += str


    return encrypted_msg

My current code only return the first letter of the message like for the 2nd example it will return only !t! I need it to return the whole message. How do I fix this?

2 Answers By Expert Tutors

By:

Jonathan S. answered • 02/21/23

Tutor
5 (3)

Experienced Math Educator and Former Google Software Engineer

Rachel M.

WHen I do move it out of the for loop is gives me an error
Report

02/21/23

Still looking for help? Get the right answer, fast.

Ask a question for free

Get a free answer to a quick problem.
Most questions answered within 4 hours.

OR

Find an Online Tutor Now

Choose an expert and meet online. No packages or subscriptions, pay only for the time you need.