[英]Hello there, can pls someone explain me how exactly I can make a solution to get the full "Hello World" encrypted into the morse code?
def encode( text, code_table, usecaps=True, insep="", outsep=" "):
for x in text:
if x != outsep:
insep += code_table[x] + outsep
return(code_table[x])
print(encode("Hello World", {'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': '--..',
'0': '-----', '1': '.----', '2': '..---',
'3': '...--', '4': '....-', '5': '.....',
'6': '-....', '7': '--...', '8': '---..',
'9': '----.'
}))
這是使用(生成器)理解和dict.get
的一種方法; 我不確定insep
和outsep
應該是什么意思。
def encode(text, code_table):
return ''.join(code_table.get(c.upper(), c) for c in text)
# output: ......-...-..--- .-----.-..-..-..
請注意,如果c.upper()
不在字典code_table
中,則 code_table.get code_table.get(..., c)
按原樣返回輸入字符c
。 特別是,將保留空格字符' '
。
實現此目的的另一種方法
#!/usr/bin/env python
code_dict = {
'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': '--..',
'1': '.----', '2': '..---', '3': '...--',
'4': '....-', '5': '.....', '6': '-....',
'7': '--...', '8': '---..', '9': '----.',
'0': '-----', ', ': '--..--', '.': '.-.-.-',
'?': '..--..', '/': '-..-.', '-': '-....-',
'(': '-.--.', ')': '-.--.-', " ": " "
}
def encode(text, code_dictionary):
# Create empty results variable to be returned once finished processing
results = ""
# For each in character in text.upper(). This will make the text uppercase so we can match our dictionary
for character in text.upper():
# This will append the value (morris code) from the character (key) from the morris code dictionary defined above
results += code_dictionary[character] + " "
return results
#print the results of the definition
print(encode("Hello World", code_dict))
您要完成的是在字典中查找每個字母。 更多關於字典的信息也可以在這里找到
請注意,我在code_dict
變量中添加了一個空格,因此我們可以在您的編碼定義中進行評估時將空格與空格匹配。
您應該嘗試解釋您的代碼不工作的方式。 但是,我懷疑問題在於您的return
語句是縮進的,因此它位於for
循環內。 這意味着 function 將在處理完第一個字符后返回。 您應該取消縮進return
,以便在for
循環完成后執行。
這是一個簡單的方法。 您可以遍歷 hw 字符串並檢查每個字母是否是 morse_dict 中的鍵。 我添加了 .upper() 字符串方法來解釋包含大小寫字母混合的字符串,而所有 dict 鍵都是大寫的。
morse_dict = {'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': '--..',
'0': '-----', '1': '.----', '2': '..---',
'3': '...--', '4': '....-', '5': '.....',
'6': '-....', '7': '--...', '8': '---..',
'9': '----.'
}
hw = 'Hello World'
for x in hw:
if x.upper() in morse_dict:
print(x, morse_dict[x.upper()])
else:
print(' ')
H ....
e .
l .-..
l .-..
o ---
W .--
o ---
r .-.
l .-..
d -..
好的,已經尋求幫助了,這是我在這里的第一個問題,我對 python 還很陌生,並且在簡單的事情上有點掙扎,但我會注意未來。 我最大的問題是代碼需要用確切的參數來解決。 insep 和 outsep 應該定義輸入和 output 分隔符,並且必須使用默認值備份。 我無法在我的代碼中真正構建您的答案,並且努力解決上面代碼中大寫字母的問題,並且不使用以下每個字母覆蓋 output。
Hrm ...假設輸入/輸出分隔符可能是這樣的。
#!/usr/bin/env python
code_dict = {
'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': '--..',
'1': '.----', '2': '..---', '3': '...--',
'4': '....-', '5': '.....', '6': '-....',
'7': '--...', '8': '---..', '9': '----.',
'0': '-----', ', ': '--..--', '.': '.-.-.-',
'?': '..--..', '/': '-..-.', '-': '-....-',
'(': '-.--.', ')': '-.--.-', " ": " "
}
def encode( text, code_table, usecaps=True, insep="", outsep=" "):
if usecaps:
text = text.upper()
else:
return 'use caps must be true'
for x in text:
if x != outsep:
insep += code_table[x] + outsep
return insep
#print the results of the definition
print(encode("Hello World", code_dict))
在 for 循環中返回code_table[x]
只會返回第一個匹配項。 這是由於返回中斷了 for 循環。
祝你學習 python 好運!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.