0
votes
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)

I want the program to ask for a number and then print its times table up to 10*number, however I keep getting this error. BTW, I'm doing GCSE computing.

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
Why are you doing b = int(b)? b is already an int. Also, what are you trying to do by +str(b)? - dorverbin
I want it to print, for example, 1 times 5 is 5, 2 times 5 is 10, etc - t.cowp
trailing + should not be there and you should also convert your output to string. Otherwise Python will not know if you want to perform concatenation of strings or addition of numbers. - chiffa
with str(b) being the 1 or the 2 in my example - t.cowp
how do you convert an output to string - t.cowp

1 Answers

1
votes

I think this is what you're trying to do:

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))

Note that +str(b) is incorrect syntax, and also note you cannot concatenate " is", which is a str, with output, which is an int.