2015-10-20 113 views
0
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计算。类型错误:坏的操作类型一元+:“STR”蟒蛇

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' 
+0

你为什么要做'b = int(b)'? 'b'已经是'int'了。另外,你想用'+ str(b)'来做什么? – dorverbin

+0

我想要它打印,例如1次5是5,2次5是10等 –

+0

尾随+不应该在那里,你也应该把你的输出转换成字符串。否则,Python将不知道是否要执行字符串连接或添加数字。 – chiffa

回答

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

+0

哦谢谢这有帮助 –

+0

听起来讽刺的 –

+0

它现在的作品! –

相关问题