我一直在尝试制作一个python morse代码转换器并且使它复杂化,我想继续我正在进行摩尔斯电码转换器的路径,但我遇到了一个问题。
Traceback (most recent call last):
File "python", line 114, in <module>
NameError: name 'firstletter' is not defined
我无法弄清楚为什么没有定义这个变量。这是代码
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")
答案 0 :(得分:3)
你的问题在于作业:
word = str(wordinput.upper)
你未能调用函数 upper ;相反,您将函数描述符分配给 word 。将该描述符转换为字符串时,word的值为
"<built-in method upper of str object at 0x7f377ea143e8>"
因此,无论输入如何,您都试图转换'&lt;'和'b';第一个没有覆盖你的列表,所以你的程序崩溃了。
更改线路以调用例程:
word = str(wordinput.upper() )
进一步的工作
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 强>
使用参考字典:
code = { 'A': ".-", 'B': "-...", etc.
}
添加一个循环来遍历字符串并转换单个字母:
for c in word:
if c in code:
morse_char = code[c]
else:
morse_char = ' '
print(morse_char)
这会挽救你的手指肌肉吗?
答案 1 :(得分:0)
我同意, 我有两个提示给你 第一: 定义if语句上方的第一个字母和第二个字母 要么 第二
使用for循环转换如下
wordinput = input("")
word = str(wordinput.upper)
for letter in range(0,len(word)+1):
if word[letter] == A:
convertedword.append(.-)
...
...
...