Python Forum
Problem with print formatting using definitions
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Problem with print formatting using definitions
#1
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
Reply
#2
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.
Reply
#3
Perhaps line 13 attempts to do what line 19 is supposed to do.

Paul
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  List of Objects print <__main. Problem Kol789 10 3,440 Jul-21-2020, 09:37 AM
Last Post: DeaD_EyE
  Highscore problem, need to print top 5 Camiosobro123 1 1,541 Jan-27-2020, 10:43 PM
Last Post: Camiosobro123
  Help Formatting Print Statement (input from 3 lists) X 2 Hebruiser 11 6,202 Dec-06-2017, 04:47 PM
Last Post: gruntfutuk

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020