Python Forum

Full Version: Problem with print formatting using definitions
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Here is my code:
def encrypt(string, s):
 
  cipher = ''
  shift = s
  for char in string: 
    if char == ' ':
      cipher = cipher + char
    elif  char.isupper():
      cipher = cipher + chr((ord(char) + shift - 65) % 26 + 65)
    else:
      cipher = cipher + chr((ord(char) + shift - 97) % 26 + 97)
  
  return print(cipher)
 
text = ("If you are having problems in this class consider using office hours on Mondays or Wednesdays")
s = int(input("enter shift number: "))
print("original string: ", text)
string = text
print("after encryption: ", encrypt(string, s))
My issue is that both the original text prints following "original string:" and the returned cipher. I need to format the cipher to print "after encryption:" I have tried moving the assignment of string until after text prints but no luck. For reference here is my current output:
enter shift number: 3
original string:  If you are having problems in this class consider using office hours on Mondays or Wednesdays
Li brx duh kdylqj sureohpv lq wklv fodvv frqvlghu xvlqj riilfh krxuv rq Prqgdbv ru Zhgqhvgdbv
after encryption:  None
Well, the encrypted message prints fine but why do you print it inside the encrypt function? The print function prints the text but returns None. Let line 13 be just
  return cipher
and it will work.
Perhaps line 13 attempts to do what line 19 is supposed to do.

Paul