1.TypeError: can't multiply sequence by non-int of type 'str'
代码案例,输入一个数字,求这个数的1~3次方
比如:输入2
输出 2,4,8
第一次写的代码如下:
#一个数的1-3次方
a = input("")
print(a)
print(a*a)
print(a*a*a)
然而出现了以下的错误:TypeError: can't multiply sequence by non-int of type 'str'
原因:类型错误:不能用str(字符串类型)做乘法,因为input()函数输入的是字符串形式,而不是数字整数形式
改正方法:将输入的数据强行改成整数int型,将第一行代码改成 a = int(input(""))
更改后代码如下:
#一个数的1-3次方
a = int(input(""))
print(a)
print(a*a)
print(a*a*a)
运行截图如下所示: