1
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'
4

2 回答 2

2

我认为这就是你想要做的:

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",这是一个str,与output,这是一个int

于 2015-10-20T13:42:10.907 回答
0
number=int(input("Please enter a number:     "))

for b in range(1,11):
    b=int(b)
    output=int (number) *int (b)
    print(b, "times",number,"is",output)
于 2021-12-31T07:25:27.713 回答