0

Why does the error occurs

try: variable = 10 print(variable + "hello") print(variable / 2) except ZeroDivisionError: print("Divided by zero") except (ValueError, TypeError): print("Error occurred") What about print(variable/2) Why this doesn't run??

27th Apr 2020, 3:54 PM
Nomita Patwal
Nomita Patwal - avatar
3 odpowiedzi
+ 3
variable is integer type there. Cannot be added to string.. Write like print( str(variable) +"hello") Edit: Nomita Patwal print(variable/2) is fine and prints 5.0 str(variable) won't change original variable, just converts in print function.. variable = str(variable) will change original variable to string equivalent.. Check in playground..
27th Apr 2020, 3:59 PM
Jayakrishna 🇮🇳
+ 3
you are trying to concentrate a number and a string. print(str(variable) + "hello")
27th Apr 2020, 4:00 PM
Bahhaⵣ
Bahhaⵣ - avatar
+ 1
Look mam. In your code, variable is a integer number and hello is a string cause it is a letter and it has " ". But we cant add a integer with a string as you did in first print statement . So you need to convert the integer into string . str(10) After this you can add both of them . On the other hand you can print both of them . Like , print(variable, "hello") This means that your are printing them one after another but not adding them. . So if you want to add them , than you have to convert them...or if you just want to print them one after one then use method 2. . Happy coding mam....
27th Apr 2020, 4:08 PM
Md. Mursalatul Islam Pallob
Md. Mursalatul Islam Pallob - avatar