-1

While doing a question of printing table I faced a problem which I wasn't able to understand...

please explain:

x=input('enter a number btw 1-12')
if(not x.isdigit() and x>13 and x<0):
    print("value should be btw 1 to 12")
else:
    y=range(1,11)
    for i in y:
        print(x*i)**

result-(when x=2)

2
22
222
2222
22222
222222
2222222
22222222
222222222
2222222222

I don't know why I am getting a series instead of

2
4
6
8
10
12
.
.
.
.
20
Tony
  • 9,672
  • 3
  • 47
  • 75

2 Answers2

1

The input function in Python always takes the input as a string.

You need to convert it to an integer using int() or float using float() before adding it.

Like this:

else:
    x = int(x)
    y=range(1,11)
    for i in y:
        print(x*i)**
Pedro Lobito
  • 94,083
  • 31
  • 258
  • 268
Thaer A
  • 2,243
  • 1
  • 10
  • 14
0

You need to specify that you input is an int and not a string. Also I would use the 'or' operator instead of 'and' in the if(x>13 or x<0):. Hope that helps!

x=int(input('enter a number btw 1-12'))

if(x>13 or x<0):
    print("value should be btw 1 to 12")

else:
    y=range(1,11)
    for i in y:
        print(x*i)
B-L
  • 144
  • 1
  • 8