TypeError:'int'对象不可下载Python 3.4.2
问题描述:
我想在Python 3.4.2中编写一段代码,它需要一串数字,将它们分开,然后将它们按相反顺序排列。到目前为止,我有:TypeError:'int'对象不可下载Python 3.4.2
#input string
numberIntegers = 123456
#assign each character to an idex
zeroDigit = numberIntegers[0]
oneDigit = numberIntegers[1]
secondDigit = numberIntegers[2]
thirdDigit = numberIntegers[3]
fourthDigit = numberIntegers[4]
fifthDigit = numberIntegers[5]
print(" The numbers to reverse: {}".format(numberIntegers))
print(" The digits, in order, are: {} {} {} {} {} {}".format(zeroDigit,oneDigit,secondDigit,thirdDigit,fourthDigit,fifthDigit))
print(" The numbers in reverse: ")
答
numberIntegers
是一个整数而不是字符串。更改如下:
numberIntegers = '123456'
您的*“输入字符串”* **不是字符串!**尝试'numberIntegers = str(123456)'或'numberIntegers ='123456''。另外,请注意'print(“这些数字的顺序是:{} {} {} {} {}”。format(* numberIntegers)'将为您节省单独的名称。 – jonrsharpe 2015-02-11 23:37:31
如果我要离开它作为一个整数,是否仍然有办法将它们分开,还是必须将它们转换为字符串? – MonotonousSonder 2015-02-11 23:55:21
你可以用数学方法做它,但是用一个字符串更容易,整数不是真的*有*数字,这就是它们以十进制形式*显示的方式(例如'0x1e240 == 123456 == 0b11110001001000000 == 0361100',那么你应该得到哪些“数字”?) – jonrsharpe 2015-02-11 23:56:49