2015-04-28 68 views
1

我想在python中编写一个程序,首先将输入的单词转换为Morse,然后将点和破折号更改为将被视为二进制数的零和破折号等 这是一个代码片段:在Python中将整数字符串转换为整数

def mimary_encode(input): 
if input.find('!')!=-1 or input.find('@')!=-1 or input.find('#')!=-1 or input.find('$')!=-1 or input.find('%')!=-1 or input.find('^')!=-1 or input.find('&')!=-1 or input.find('*')!=-1 or input.find('(')!=-1 or input.find(')')!=-1 or input.find('_')!=-1 or input.find('-')!=-1 or input.find('=')!=-1 or input.find('+')!=-1 or input.find('.')!=-1 or input.find('"')!=-1 or input.find("'")!=-1 or input.find(',')!=-1 or input.find(' ')!=-1 or input.find(';')!=-1 or input.find(':')!=-1 or input.find('[')!=-1 or input.find(']')!=-1 or input.find('{')!=-1 or input.find('}')!=-1 or input.find('?')!=-1 or input.find('<')!=-1 or input.find('>')!=-1: 
    print "Inputs cannot contain spaces or symbols" 
else:base=input 
nol=len(input) 
if base.find("a")!=-1: 
    base=base.replace("a",".-") 
if base.find("b")!=-1: 
    base=base.replace("a","-...") 
if base.find("c")!=-1: 
    base=base.replace("c","-.-.") 
if base.find("d")!=-1: 
    base=base.replace("d","-..") 
if base.find("e")!=-1: 
    base=base.replace("e",".") 
if base.find("f")!=-1: 
    base=base.replace("f","..-.") 
if base.find("g")!=-1: 
    base=base.replace("g","--.") 
if base.find("h")!=-1: 
    base=base.replace("h","....") 
if base.find("i")!=-1: 
    base=base.replace("i","..") 
if base.find("j")!=-1: 
    base=base.replace("j",".---") 
if base.find("k")!=-1: 
    base=base.replace("k","-.-") 
if base.find("l")!=-1: 
    base=base.replace("l",".-..") 
if base.find("m")!=-1: 
    base=base.replace("m","--") 
if base.find("n")!=-1: 
    base=base.replace("n","-.") 
if base.find("o")!=-1: 
    base=base.replace("o","---") 
if base.find("p")!=-1: 
    base=base.replace("p",".--.") 
if base.find("q")!=-1: 
    base=base.replace("q","--.-") 
if base.find("r")!=-1: 
    base=base.replace("r",".-.") 
if base.find("s")!=-1: 
    base=base.replace("s","...") 
if base.find("t")!=-1: 
    base=base.replace("t","-") 
if base.find("u")!=-1: 
    base=base.replace("u","..-") 
if base.find("v")!=-1: 
    base=base.replace("v","...-") 
if base.find("w")!=-1: 
    base=base.replace("w",".--") 
if base.find("x")!=-1: 
    base=base.replace("x","-..-") 
if base.find("y")!=-1: 
    base=base.replace("y","-.--") 
if base.find("z")!=-1: 
    base=base.replace("z","--..") 
if base.find("1")!=-1: 
    base=base.replace("1",".----") 
if base.find("2")!=-1: 
    base=base.replace("2","..---") 
if base.find("3")!=-1: 
    base=base.replace("3","...--") 
if base.find("4")!=-1: 
    base=base.replace("4","....-") 
if base.find("5")!=-1: 
    base=base.replace("5",".....") 
if base.find("6")!=-1: 
    base=base.replace("6","-....") 
if base.find("7")!=-1: 
    base=base.replace("7","--...") 
if base.find("8")!=-1: 
    base=base.replace("8","---..") 
if base.find("9")!=-1: 
    base=base.replace("9","----.") 
if base.find("0")!=-1: 
    base=base.replace("0","-----") 
if base.find("-")!=-1: 
    base=base.replace("-","0") 
if base.find(".")!=-1: 
    base=base.replace(".","1") 
int(base) 


mimary_encode("hi") 

我知道这可能是不写它的最佳方式,但问题是错误蟒蛇一直给我的是:

Traceback (most recent call last): 
    File "C:/Documents and Settings/Moshe's Programming/Desktop/Python  
Projects/Mimary/Mimary attempt 1.py", line 86, in <module> 
    mimary_encode("hi") 
    File "C:/Documents and Settings/Moshe's Programming/Desktop/Python   
Projects/Mimary/Mimary attempt 1.py", line 83, in mimary_encode 
print base + 1 
TypeError: cannot concatenate 'str' and 'int' objects 

是什么这个错误的意思?我该如何解决这个错误?我已经把基地变成了一个整数 - 不是吗?

+2

int(“base”)是什么意思?你不能让一个“基数”为整数 – RafaelC

+1

你不需要所有这些if语句。如果找不到一封信,替换将不会执行任何操作。也看看翻译功能。 – Loocid

+0

“base”是一个变量 – JediPythonClone

回答

1

虽然你的代码被重新搞砸了,但它的工作原理。但是,您的第一个错误是由于行int("base")而引起的。

如果你写int("base")你试图把字符串“base”变成一个整数,这是不可能做到的。

然后,你改变了代码print base + 1这也是不可能做到的,一旦basestring,你不能用+标志总结字符串和整数。 所以,你想要做的是:

def mimary_encode(base): 
    #Dowhateveryouwant 
    return int(base) #Only if you are sure base contains only integers 
print mimary_encode("hi") 
+0

是的,但我想把整数字符串作为一个整数 – JediPythonClone

+0

如果字符串只包含数字,那么'int(base)'将完成这项工作 – RafaelC

+0

它应该只包含数字看我如何将所有字符更改为一个或零个 – JediPythonClone

0

错误来自print base + 1,其中base是一个字符串和1的整数到来。

这是您的函数的替代实现。首先,我将摩尔斯电码编码定义为字典。在函数中,我首先将所有字母转换为小写。然后,我使用get字典函数返回莫尔斯码值,如果它在字典中,则使用空字符串进行过滤。这与过滤不良数据的原始方法不同。在这里,我只查找字典中的数据。最后,我使用生成器将编码的字母连接在一起:code = " ".join((morse.get(c, "") for c in input_string)),它与列表理解类似,但对于大字符串更有效。

from string import letters 

msg = 'I hear 13 knights from the Round Table are here!!!' 

def mimary_encode(input_string): 
    input_string = ''.join([c.lower() if c in letters else c 
          for c in input_string]) 
    code = " ".join((morse.get(c, "") for c in input_string)) 
    return code 

morse = { 
'0': '-----', 
'1': '.----', 
'2': '..---', 
'3': '...--', 
'4': '....-', 
'5': '.....', 
'6': '-....', 
'7': '--...', 
'8': '---..', 
'9': '----.', 
'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': '--..'} 

对邮件进行编码(早先定义为msg):

reverse_morse = {v: k for k, v in morse.iteritems()} 
:鉴于你的字典的一个一对一的映射

>>> mimary_encode(msg) 
'.. .... . .- .-. .---- ...-- -.- -. .. --. .... - ... ..-. .-. --- -- - .... . .-. --- ..- -. -.. - .- -... .-.. . .- .-. . .... . .-. .' 

,您可以使用字典解析扭转它

然后,您可以反转莫尔斯电码以将其转换回字母/数字字符串。

>>> ''.join([reverse_morse.get(c, "") for c in mimary_encode(msg).split(" ")]) 
'ihear13knightsfromtheroundtablearehere' 

请注意,所有字母都转换为小写字母,并且感叹号已被删除。

+0

我真的不明白我喜欢用代码的措辞我明白 – JediPythonClone

+0

哪部分?列表/字典理解的发电机? – Alexander

+0

加入白色空间和味精 – JediPythonClone