Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 如何从输入中分解一个单词,并将其应用于已建立字典的键:值对的打印函数 我想把输入的单词翻译成摩尔斯电码。我的代码只适用于字母“a”,其他什么都不适用。 词典_Python 3.x - Fatal编程技术网

Python 3.x 如何从输入中分解一个单词,并将其应用于已建立字典的键:值对的打印函数 我想把输入的单词翻译成摩尔斯电码。我的代码只适用于字母“a”,其他什么都不适用。 词典

Python 3.x 如何从输入中分解一个单词,并将其应用于已建立字典的键:值对的打印函数 我想把输入的单词翻译成摩尔斯电码。我的代码只适用于字母“a”,其他什么都不适用。 词典,python-3.x,Python 3.x,dic={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”:“-…”} 到目前为止不起作用的代码 def Morse(): 位={} inp=str(输入(“键入您想要转换为摩尔斯电码的内容(仅限字母):”) 对于键,di

dic={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”:“-…”}

到目前为止不起作用的代码
def Morse():
位={}
inp=str(输入(“键入您想要转换为摩尔斯电码的内容(仅限字母):”)
对于键,dic.items()中的值:
如果len(inp)>1:
bit=list(inp.upper())#将输入转换为字母
对于k位:
如果k==键:
打印(k,值)
打破
elif inp.upper()==键:#尝试在此处同时应用字母的大写和小写
位=列表(inp)
打印(位、值)
其他:
打破
具有输入功能的所需输出:

键入您希望转换为摩尔斯电码的内容(仅限字母):ads

“a”、“d”、“s”:“-”、“、”、“…”

谢谢你的意见。我终于想出了我想用它做什么:

def Morse():
位=[]
inp=str(输入(“键入您想要转换为摩尔斯电码的内容(仅限字母):”)
对于键,dic.items()中的值:
如果len(inp)>1:
位=列表(inp)
拼写=列表(映射(dic.get,位))
打印(拼写)
打破

对不起,这个问题措词不当。第一次发布到stack并熟悉界面。

我不确定您想要什么,但此代码确保用户输入的仅是使用
regex
while loop
的字母,然后返回转换的
列表

dic = {"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" : "--.."}

import re
def Morse():
    lst=[]
    while True:
        inp=str(input("Type what you would like converted to Morse code (letters only):"))
        if not re.match("^[A-Za-z]*$", inp):
            print ("Error! Make sure you only use letters")
        else:
            break
    
    for i in inp:
        lst.append(dic[i.uppe()]
    return lst
Morse()
>>>ads
>>>['.-', '.', '...']

您可以在输入本身上循环,而不是在字典上循环

def Morse():
    bit = {}
    #don't need to convert input to string as it is always string
    inp = input("Type what you would like converted to Morse code") 
    for letter in input:
        bit.update({letter:dic[letter.upper()]})
    print(bit)
或者使用字典理解

def Morse():
    inp = input("Type what you would like converted to Morse code") 
    bit = {letter:dic[letter.upper()] for letter in inp}
    print(bit)

这可以作为一个班轮:

def morse():
打印([dic[char]表示列表中的char(输入(“键入您希望转换为摩尔斯电码的内容(仅限字母):”)。如果dic中的char为upper()))
或者以更漂亮的格式:

def morse():
印刷品([
dic[字符]
对于列表中的字符(输入(
“键入要转换为摩尔斯电码的内容(仅限字母):”
).upper())
dic中的if字符
])

你应该解释什么东西不起作用(你得到了什么输出等)。我不明白你想要什么作为输出,你能解释更多吗?使用:
python def Morse():bit=[]inp=str(输入(“键入你想要转换成莫尔斯码(仅字母):”)的键,dic中的值。items():if len(inp)>1:bit=list(inp)spell=list(map(dic.get,bit))打印(拼写)中断
OUTPUT:键入您想要转换为莫尔斯电码的内容(仅字母):ADS['.-','.','.,'.'.'.']