Python Forum
Problem with print formatting using definitions - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Homework (https://python-forum.io/forum-9.html)
+--- Thread: Problem with print formatting using definitions (/thread-32977.html)



Problem with print formatting using definitions - Tberr86 - Mar-20-2021

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



RE: Problem with print formatting using definitions - Serafim - Mar-20-2021

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.


RE: Problem with print formatting using definitions - DPaul - Mar-20-2021

Perhaps line 13 attempts to do what line 19 is supposed to do.

Paul