在Python中将整数字符串转换为整数

莫西·戈德堡(Moshe Goldberg)

我试图用python编写程序,方法是先将输入的单词转到Morse,然后将点和破折号更改为1和0,将其视为二进制数,从而对项目进行编码。这是一个代码段:

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")

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

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

这个错误是什么意思?如何解决此错误?我已经把基数变成整数了,不是吗?

拉斐尔克

尽管您的代码被彻底搞砸了,但它仍然可以工作。但是,由于发生,您的第一个错误出现了int("base")

如果编写int("base"),则尝试将字符串“ base”转换为整数,这是不可能做到的。

然后,您将代码更改print base + 1为也是不可能的,一次base是a,string并且您不能将带+符号的字符串和整数相加因此,您想要做的是:

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

在方案中将字符串形式的数字转换为整数

来自分类Dev

在python中将数字字符串转换为整数列表

来自分类Dev

在Python中将字符串转换为整数

来自分类Dev

在python中将字符串量转换为整数量

来自分类Dev

如何在Rust中将数字字符串转换为整数数组或整数向量?

来自分类Dev

Python字符串转换为整数

来自分类Dev

如何在Swift中将50位数字的字符串转换为适当的整数类型?

来自分类Dev

在PHP中将字符串[基本上是逗号分隔的数字]转换为整数的最佳方法

来自分类Dev

在Shopify Liquid中将字符串转换为整数?

来自分类Dev

在AngularJS视图中将字符串转换为整数

来自分类Dev

如何在Bash中将字符串转换为整数?

来自分类Dev

在Scheme中将字符串转换为整数

来自分类Dev

如何在Rust中将字符串转换为整数?

来自分类Dev

在Java中将带空格的字符串转换为整数

来自分类Dev

在PHP中将JSON字符串转换为整数

来自分类Dev

在C中将gets()字符串转换为整数

来自分类Dev

如何在PowerShell中将字符串转换为整数

来自分类Dev

在Kotlin中将字符串转换为整数列表

来自分类Dev

在Shell中将字符串转换为整数

来自分类Dev

在Android中将字符串转换为整数

来自分类Dev

如何在CakePHP中将整数转换为字符串?

来自分类Dev

如何在PHP中将字符串转换为整数

来自分类Dev

在Rails的CSV导入中将字符串转换为整数

来自分类Dev

@如何在Perl中将字符串转换为整数

来自分类Dev

在 PowerBI 中将整数转换为字符串并连接

来自分类Dev

将非数字字符串转换为整数?

来自分类Dev

从字符串转换为数字,但只转换为有效整数的字符串

来自分类Dev

在Python中将整数数组转换为颜色字符串或颜色值的快速方法

来自分类Dev

如何在python中将列表中的整数转换为字符串

Related 相关文章

  1. 1

    在方案中将字符串形式的数字转换为整数

  2. 2

    在python中将数字字符串转换为整数列表

  3. 3

    在Python中将字符串转换为整数

  4. 4

    在python中将字符串量转换为整数量

  5. 5

    如何在Rust中将数字字符串转换为整数数组或整数向量?

  6. 6

    Python字符串转换为整数

  7. 7

    如何在Swift中将50位数字的字符串转换为适当的整数类型?

  8. 8

    在PHP中将字符串[基本上是逗号分隔的数字]转换为整数的最佳方法

  9. 9

    在Shopify Liquid中将字符串转换为整数?

  10. 10

    在AngularJS视图中将字符串转换为整数

  11. 11

    如何在Bash中将字符串转换为整数?

  12. 12

    在Scheme中将字符串转换为整数

  13. 13

    如何在Rust中将字符串转换为整数?

  14. 14

    在Java中将带空格的字符串转换为整数

  15. 15

    在PHP中将JSON字符串转换为整数

  16. 16

    在C中将gets()字符串转换为整数

  17. 17

    如何在PowerShell中将字符串转换为整数

  18. 18

    在Kotlin中将字符串转换为整数列表

  19. 19

    在Shell中将字符串转换为整数

  20. 20

    在Android中将字符串转换为整数

  21. 21

    如何在CakePHP中将整数转换为字符串?

  22. 22

    如何在PHP中将字符串转换为整数

  23. 23

    在Rails的CSV导入中将字符串转换为整数

  24. 24

    @如何在Perl中将字符串转换为整数

  25. 25

    在 PowerBI 中将整数转换为字符串并连接

  26. 26

    将非数字字符串转换为整数?

  27. 27

    从字符串转换为数字,但只转换为有效整数的字符串

  28. 28

    在Python中将整数数组转换为颜色字符串或颜色值的快速方法

  29. 29

    如何在python中将列表中的整数转换为字符串

热门标签

归档