Python Forum
I always get 'None' returned. Confused. What did I miss?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
I always get 'None' returned. Confused. What did I miss?
#1
import random


# Morse Code string translator
# delimited with spaces for characters, and / for words
# format example "HELLO WORLD": .... . .-.. .-.. --- / .-- --- .-. .-.. -..
def untranslate_morse_code_string(code_string):

    InvertmorseAlphabet = {

        " ": "/", "A": ".-", "C": "-.-.", "B": "-...", "E": ".", "D": "-..",
        "G": "--.", "F": "..-.", "I": "..", "H": "....", "K": "-.-", "J": ".---",
        "M":  "--", "L": ".-..","O": "---", "N": "-.", "Q": "--.-", "P": ".--.",
        "S": "...", "R": ".-.", "U": "..-", "T": "-", "W": ".--", "V": "...-",
        "Y": "-.--", "X": "-..-", "Z": "--..",
        "1": ".----", "2": "..---", "3": "...--", "4": "....-", "5": ".....",
        "6": "-....", "7": "--...", "8": "---..", "9": "----.", "0": "-----"
    }
    print (InvertmorseAlphabet)
    print (code_string)


def translate_morse_code_string(code_string):

    morseAlphabet = {
        "/": " ", ".-": "A", "-.-.": "C", "-...": "B", ".": "E", "-..": "D",
        "--.": "G", "..-.": "F", "..": "I", "....": "H", "-.-": "K", ".---": "J",
        "--":  "M", ".-..": "L","---": "O", "-.": "N", "--.-": "Q", ".--.": "P",
        "...": "S", ".-.": "R", "..-": "U", "-": "T", ".--": "W", "...-": "V",
        "-.--": "Y", "-..-": "X", "--..": "Z",
        ".----": "1", "..---": "2", "...--": "3", "....-": "4", ".....": "5",
        "-....": "6", "--...": "7", "---..": "8", "----.": "9", "-----": "0"
    }



Consonant = [ 'B', 'C', 'D', 'F',  'G', 'H', 'J', 'K', 'L', 'M', 'N', 'P',  'Q', 'W', 'X', 'Z']
Vowel = ['A', 'E', 'I', 'O', 'U', 'Y'] # Sometimes y and w. Need more vowels
Number = [ '0', '1', '2', '3',  '4', '5', '6', '7', '8', '9' ]
#
# This below works
#
input_msg=random.choice(Vowel)+random.choice(Number)+random.choice(Consonant)
string_len=len(input_msg)
print(input_msg,string_len)
#
# End of this works
#
x=input_msg[2]
character_output = untranslate_morse_code_string(x)
print (x, character_output)
#
#What happens if I force a particular letter
#
x='G'
character_output = untranslate_morse_code_string(x)
print (x, character_output)
#
#What happens if I use the translate on a particular code
#
x="..."
character_output = translate_morse_code_string(x)
print (x, character_output)
Reply
#2
You need a return statement. If you don't explicitly return something with a return statement, None is returned by default. Your functions don't seem to be doing anything, so I'm not sure exactly what they should return.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
Duh!

This is what happens when you copy and modify a routine.

I left off all the code. I wasn't paying attention to what I was doing and was overtired.

Thanks. It works fine with the rest of the code in it.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  TypeError: __str__ returned non-string (type tuple) Anldra12 1 1,044 Apr-13-2021, 07:50 AM
Last Post: Anldra12
  Confused with 'flags' tester_V 10 1,011 Apr-12-2021, 03:03 AM
Last Post: tester_V
  Simple Tic Tac Toe but I'm confused Izith 1 531 Sep-26-2020, 04:42 PM
Last Post: Larz60+
  I am really confused with this error. Runar 3 660 Sep-14-2020, 09:27 AM
Last Post: buran
  TypeError: __repr__ returned non-string (type dict) shockwave 0 1,143 May-17-2020, 05:56 PM
Last Post: shockwave
  Confused by 'break' in the official documents Chuck_Norwich 2 764 Apr-12-2020, 09:26 PM
Last Post: Chuck_Norwich
  Exception: Returned Type Mismatch Error devansing 1 1,772 Mar-06-2020, 07:26 PM
Last Post: ndc85430
  Confused on how to go about writing this or doing this... pythonforumuser 3 866 Feb-10-2020, 09:15 AM
Last Post: snippsat
  How to use a returned value? t4keheart 12 1,690 Jan-16-2020, 06:54 AM
Last Post: perfringo
  Strange Characters in JSON returned string fioranosnake 4 1,873 Dec-02-2019, 07:25 PM
Last Post: fioranosnake

Forum Jump:

User Panel Messages

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