-3

I am using Python Version 3.6.4

I was trying to write a basic python code in Jupyter Notebook where I found my code acting funny.

The below given code is working as expected But when I change the operation to (+) in the 4th line of code print( x, '+', y, '=', x+y) then it is resulting with Error.

Question is why is this unexpected behavior happening when there is a change of operator where multiplication works fine and addition results with error?

def fuc(x):
    x = input('Enter the number:')

    for y in range(1,11):
        print( x, 'x', y, '=', x*y)
print(fuc(2))
Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
  • 3
    Do not post images of text. – user3942918 Jan 04 '19 at 08:35
  • 1
    And please indent your code properly. This is crucial in Python. – Nisse Engström Jan 04 '19 at 08:38
  • Does this answer your question? [TypeError: Can only concatenate str (not "int") to str (simple Python programme)](https://stackoverflow.com/questions/52225721/typeerror-can-only-concatenate-str-not-int-to-str-simple-python-programme). Short answer: Multiplication between strings and ints is defined - returns the string concatenated to itself int times. Addition of string and int is not defined – Tomerikoo Oct 15 '20 at 14:06

2 Answers2

2

The user input (i.e. x) is string. y is integer. Multiplication between string and integer is valid python operation. Addition between integer and string is not. Note that I doubt your code with multiplication works as expected, i.e. it will not multiply the number, but repeat the string, e.g.

>>> '3' * 4
'3333'

To deal with the problem you need to convert the user input to int:

x = int(input('Enter the number:'))

Note that this will not handle any invalid input, e.g. not numeric input and will raise an exception.

EDIT: Include example code snippet:

def fuc(x):
    x = int(input('Enter the number:'))
    for y in range(1,11):
        print(x, '+', y, '=', x+y)
        # print(f'{x} + {y} = {x+y}') # in 3.6+ you better use this

fuc(2)

output in python3

Enter the number:3
3 + 1 = 4
3 + 2 = 5
3 + 3 = 6
3 + 4 = 7
3 + 5 = 8
3 + 6 = 9
3 + 7 = 10
3 + 8 = 11
3 + 9 = 12
3 + 10 = 13
>>>

Normally I would use string formatting for the print, but in this case I keep as in the original code

buran
  • 13,682
  • 10
  • 36
  • 61
  • Agreed with your answer and I tried the way you suggested but didn't work. but when I tried the same with Python2.7 it is working properly. – Harsha Varanasi Jan 04 '19 at 09:41
  • I don't know what you have tried, but **it woks** - check my updated answer. – buran Jan 04 '19 at 09:44
  • just looked at your [other question](https://stackoverflow.com/questions/50799375/addition-function-in-python-is-not-working-as-expected) and there you do convert user input to float and use string formatting. Why don't you do the same here? – buran Jan 04 '19 at 09:54
  • Thank you guys it's working after restarting my Jupyter notebook – Harsha Varanasi Jan 04 '19 at 11:48
0

you can try this piece of code:

def fuc(x):
x = float(input('please enter your desired number'))

for i in range(1,11):#generally i is used as an iterable
    print( "{}*{} '=' ",x*i)
print(fuc())
  • Welcome to Stack Overflow. Unfortunately, on this occasion, your indentation is wrong. Fixing the indentation causes your code to fail with a TypeError. Fixing that error causes it to output, e.g. `{}*{} '=' 12.0`. Finally, the OP's question was "Why is this happening?" which can't be answered with a code-only answer. Could you please read [How to Write a Good Answer](https://stackoverflow.com/help/how-to-answer) and in future make sure that answers both answer the question and are technically correct. Thanks. – David Buck May 09 '20 at 00:57