私はPythonのモールスコード翻訳者を作ろうとしてきましたが、それを複雑にしています。私はモールスコード翻訳者のために進めている道を続けたいと思いますが、問題に遭遇しました。この変数がPythonで定義されていないのはなぜですか?
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")
良い以下のように変換するforループを使用している場合firstletterと上記第二の手紙を定義します主よ、辞書を使ってください!そしてループ! – kindall
実行されたすべてのコードがその変数を作成(ヒント)するかどうかは不明です。 – usr2564301
これは私の目を傷つける。辞書を使う! – dawg