【发布时间】:2015-10-20 13:36:01
【问题描述】:
number=int(input("Please enter a number: "))
for b in range(1,11):
b=int(b)
output=int (number) *int (b)
print(+str (b) +" times "+ str (number) +" is " +output)
我希望程序询问一个数字,然后打印它的时间表,最多为 10*number,但是我不断收到此错误。顺便说一句,我正在做 GCSE 计算。
Traceback (most recent call last):
File "C:\Users\jcowp_000\Documents\School\Lutterworth\Computing\Documents_-_-___________---________---______-_-_-_-__-__\Python\Python manual tasks.py", line 21, in <module>
print(+str (b) +" times "+ str (number) +" is " +output)
TypeError: bad operand type for unary +: 'str'
【问题讨论】:
-
你为什么要
b = int(b)?b已经是int。另外,+str(b)你想做什么? -
我要它打印,比如1乘5就是5,2乘5就是10,等等
-
尾随 + 不应该在那里,您还应该将输出转换为字符串。否则 Python 将不知道您是要执行字符串连接还是数字相加。
-
在我的例子中 str(b) 是 1 或 2
-
如何将输出转换为字符串