Why is this variable not defined in Python?

710 views Asked by At

I've been trying to make a python morse code translator and am over complicating it, I would like to continue on the path that I am going on for the morse code translator but I ran into a problem.

Traceback (most recent call last):
  File "python", line 114, in <module>
NameError: name 'firstletter' is not defined

I couldn't figure out why this variable was not being defined. Here's the code

wordinput = input("What do you want to convert")
word = str(wordinput.upper)

if word[0] == 'A':
    firstletter = ".-"
elif word[0] == "B":
    firstletter = "-..."
elif word[0] == 'C':
    firstletter = '-.-.'
elif word[0] == 'D':
    firstletter = '-..'
elif word[0] == 'E':
    firstletter = '.'
elif word[0] == 'F':
    firstletter = '..-.'
elif word[0] == 'G':
    firstletter = '--.'
elif word[0] == "H":
    firstletter = '....'
elif word[0] == "I":
    firstletter = '..'
elif word[0] == 'J':
    firstletter = ".."
elif word[0] == 'K':
    firstletter = '-.-'
elif word[0] == 'L':
    firstletter = '.-..'
elif word[0] == 'M':
    firstletter = '--'
elif word[0] == 'N':
    firstletter = '-.'
elif word[0] == 'O':
    firstletter = '---'
elif word[0] == 'P':
    firstletter = '.--.'
elif word[0] == 'Q':
    firstletter = '--.-'
elif word[0] == 'R':
    firstletter = '.-.'
elif word[0] == 'S':
    firstletter = '...'
elif word[0] == 'T':
    firstletter = '-'
elif word[0] == 'U':
    firstletter = '..-'
elif word[0] == 'V':
    firstletter = '...-'
elif word[0] == 'W':
    firstletter = '.--'
elif word[0] == 'X':
    firstletter = '-..-'
elif word[0] == 'Y':
    firstletter = '-.--'
elif word[0] == 'Z':
    firstletter = '--..'

if word[1] == 'A':
    secondletter = ".-"
elif word[1] == "B":
    secondletter = "-..."
elif word[1] == 'C':
    secondletter = '-.-.'
elif word[1] == 'D':
    secondletter = '-..'
elif word[1] == 'E':
    secondletter = '.'
elif word[1] == 'F':
    secondletter = '..-.'
elif word[1] == 'G':
    secondletter = '--.'
elif word[1] == "H":
    secondletter = '....'
elif word[1] == "I":
    secondletter = '..'
elif word[1] == 'J':
    secondletter = ".."
elif word[1] == 'K':
    secondletter = '-.-'
elif word[1] == 'L':
    secondletter = '.-..'
elif word[1] == 'M':
    secondletter = '--'
elif word[1] == 'N':
    secondletter = '-.'
elif word[1] == 'O':
    secondletter = '---'
elif word[1] == 'P':
    secondletter = '.--.'
elif word[1] == 'Q':
    secondletter = '--.-'
elif word[1] == 'R':
    secondletter = '.-.'
elif word[1] == 'S':
    secondletter = '...'
elif word[1] == 'T':
    secondletter = '-'
elif word[1] == 'U':
    secondletter = '..-'
elif word[1] == 'V':
    secondletter = '...-'
elif word[1] == 'W':
    secondletter = '.--'
elif word[1] == 'X':
    secondletter = '-..-'
elif word[1] == 'Y':
    secondletter = '-.--'
elif word[1] == 'Z':
    secondletter = '--..'





print(firstletter + secondletter)
import os
os.system("pause")
2

There are 2 answers

4
Prune On BEST ANSWER

Your problem is with the assignment:

word = str(wordinput.upper)

You failed to call the function upper; instead, you assigned the function descriptor to word. When you convert that descriptor to a string, the value for word is

"<built-in method upper of str object at 0x7f377ea143e8>"

Thus, regardless of the input, you're trying to convert '<' and 'b'; the first one has no coverage in your list, so your program crashes.

Change the line to call the routine:

word = str(wordinput.upper() )

FURTHER WORK

The problem will still occur if the user enters any input other than pure letters.  Add a line before your long **if** to give a default value, whatever you'd like to see printed if the input isn't a letter.  I use a space here:

firstletter = ' '

IMPROVEMENTS

Use a reference dictionary:

code = { 'A': ".-", 'B': "-...", etc.
       }

The add a loop to go through the string and convert the individual letters:

for c in word:
    if c in code:
        morse_char = code[c]
    else:
        morse_char = ' '
    print(morse_char)

Does that save your finger muscles?

0
Chris Howe On

I agree, I have two tips for you first: define firstletter and second letter above the if statements or second

Use a for loop to convert like below

wordinput = input("")
word = str(wordinput.upper)

for letter in range(0,len(word)+1):
    if word[letter] == A:
        convertedword.append(.-)
...
...
...