Python Forum
Passing print output into another print statement
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Passing print output into another print statement
#1
Hi, I'm trying to pass the output of a print(result) statement into a ( for i in anagrams("")) to another print statement and then run the code. So that print(i) automatically uses the word [do] from the(result). I'm trying to generate anagrams from Morse code. At the moment the code below needs me to manually insert the word (do). Is this even possible to automate with the code I have? Print statements are at bottom of code.

Thanks for your help



# Python program to implement Morse Code Translator 

''' 
VARIABLE KEY 
'cipher' -> 'stores the morse translated form of the english string' 
'decipher' -> 'stores the english translated form of the morse string' 
'citext' -> 'stores morse code of a single character' 
'i' -> 'keeps count of the spaces between morse characters' 
'message' -> 'stores the string to be encoded or decoded' 
'''

# Dictionary representing the morse code chart 
MORSE_CODE_DICT = { 'A':'.-', 'B':'-...', 
					'C':'-.-.', 'D':'-..', 'E':'.', 
					'F':'..-.', 'G':'--.', 'H':'....', 
					'I':'..', 'J':'.---', 'K':'-.-', 
					'L':'.-..', 'M':'--', 'N':'-.', 
					'O':'---', 'P':'.--.', 'Q':'--.-', 
					'R':'.-.', 'S':'...', 'T':'-', 
					'U':'..-', 'V':'...-', 'W':'.--', 
					'X':'-..-', 'Y':'-.--', 'Z':'--..', 
					'1':'.----', '2':'..---', '3':'...--', 
					'4':'....-', '5':'.....', '6':'-....', 
					'7':'--...', '8':'---..', '9':'----.', 
					'0':'-----', ', ':'--..--', '.':'.-.-.-', 
					'?':'..--..', '/':'-..-.', '-':'-....-', 
					'(':'-.--.', ')':'-.--.-'} 

# Function to encrypt the string 
# according to the morse code chart 
def encrypt(message): 
	cipher = '' 
	for letter in message: 
		if letter != ' ': 

			# Looks up the dictionary and adds the 
			# correspponding morse code 
			# along with a space to separate 
			# morse codes for different characters 
			cipher += MORSE_CODE_DICT[letter] + ' '
		else: 
			# 1 space indicates different characters 
			# and 2 indicates different words 
			cipher += ' '

	return cipher 

# Function to decrypt the string 
# from morse to english 
def decrypt(message): 

	# extra space added at the end to access the 
	# last morse code 
	message += ' '

	decipher = '' 
	citext = '' 
	for letter in message: 

		# checks for space 
		if (letter != ' '): 

			# counter to keep track of space 
			i = 0

			# storing morse code of a single character 
			citext += letter 

		# in case of space 
		else: 
			# if i = 1 that indicates a new character 
			i += 1

			# if i = 2 that indicates a new word 
			if i == 2 : 

				# adding space to separate words 
				decipher += ' '
			else: 

				# accessing the keys using their values (reverse of encryption) 
				decipher += list(MORSE_CODE_DICT.keys())[list(MORSE_CODE_DICT 
				.values()).index(citext)] 
				citext = '' 

	return decipher 

# Hard-coded driver function to run the program 
def main(): 
	
	message = "-.. ---"
	result = decrypt(message) 
	print (result) 
def anagrams(word):
    """ Generate all of the anagrams of a word. """ 
    if len(word) < 2:
        yield word
    else:
        for i, letter in enumerate(word):
            if not letter in word[:i]: #avoid duplicating earlier words
                for j in anagrams(word[:i]+word[i+1:]):
                    yield j+letter
                    
# Executes the main function 
if __name__ == '__main__': 
	main() 
for i in anagrams("do"):
        print (i)
Reply
#2
print does not return the value that it outputs to the screen (it returns None). So, in the functions where you're printing, just return the value you want to use instead and then pass those to print.
Reply
#3
(Sep-08-2019, 10:14 AM)ndc85430 Wrote: print does not return the value that it outputs to the screen (it returns None). So, in the functions where you're printing, just return the value you want to use instead and then pass those to print.

Can you please show an example. thanks
Reply
#4
>>> def do_some_work():
...     x = 7
...     y = 5
...     z = 2 * x + y
...     return z  
... 
>>> def do_some_more_work(x):
...     return (x - 5.0) / 20
... 
>>> result = do_some_work()
>>> final_result = do_some_more_work(result)
>>> print(final_result)
0.7
>>> 
The functions do_some_work and do_some_more_work calculate some values. I could have called print in either one, but then the result of the calculation wouldn't be available to use in further computation. For example, if I'd called print in do_some_work instead of returning the value, result would be None and the subsequent call to do_some_more_work wouldn't work.
Reply
#5
I still cannot seem to get my print job to work, can anyone show me an example from my own code the correct way to print so that the anagrams print with out manually inserting the [word]. Thanks a bunch:)
Reply
#6
(Sep-08-2019, 12:31 PM)Pleiades Wrote: I still cannot seem to get my print job to work, can anyone show me an example from my own code the correct way to print so that the anagrams print with out manually inserting the [word]. Thanks a bunch:)
Return result from main function, and use it in your loop, here is the code:
def main(): 
    message = "-.. ---"
    result = decrypt(message) 
    print (result)
    return result # return result
for i in anagrams(main()):
    print (i)
Reply
#7
(Sep-08-2019, 02:09 PM)luoheng Wrote:
(Sep-08-2019, 12:31 PM)Pleiades Wrote: I still cannot seem to get my print job to work, can anyone show me an example from my own code the correct way to print so that the anagrams print with out manually inserting the [word]. Thanks a bunch:)
Return result from main function, and use it in your loop, here is the code:
def main(): 
    message = "-.. ---"
    result = decrypt(message) 
    print (result)
    return result # return result
for i in anagrams(main()):
    print (i)

Thanks so much luoheng I was really struggling with this code. You are a great help and a nice person. Peace Man!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to Randomly Print a Quote From a Text File When User Types a Command on Main Menu BillKochman 13 752 Apr-18-2024, 07:07 AM
Last Post: Bronjer
  print doesnt work in a function ony 2 299 Mar-11-2024, 12:42 PM
Last Post: Pedroski55
  print(0.1+0.2==0.3) akbarza 3 400 Mar-04-2024, 02:02 PM
Last Post: Gribouillis
  problem with print command in super() akbarza 5 593 Feb-01-2024, 12:25 PM
Last Post: deanhystad
  problem with spliting line in print akbarza 3 382 Jan-23-2024, 04:11 PM
Last Post: deanhystad
  problem with print lists MarekGwozdz 4 680 Dec-15-2023, 09:13 AM
Last Post: Pedroski55
  What a difference print() makes Mark17 2 564 Oct-20-2023, 10:24 PM
Last Post: DeaD_EyE
Question Common understanding of output processing with conditional statement neail 6 881 Sep-17-2023, 03:58 PM
Last Post: neail
  elif not responding on print EddieG 3 867 Jul-20-2023, 09:44 PM
Last Post: Pedroski55
  print(data) is suddenly invalid syntax db042190 6 1,195 Jun-14-2023, 02:55 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

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