TypeError:一元+的错误操作数类型:' str'蟒蛇

时间:2015-10-20 13:36:02

标签: python operands

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 *数字,但是我不断收到此错误。顺便说一句,我正在进行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'

1 个答案:

答案 0 :(得分:1)

我认为这就是你要做的事情:

number = int(input("Please enter a number:     "))

for b in range(1,11):
    output = int(number) * b   # b is already an int, you can use it directly in computations
    print(str(b) + " times " + str(number) + " is " + str(output))

请注意,+str(b)语法不正确,同时请注意,您无法将" is" stroutput连接起来,int