Python error with int | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Python error with int

# your code goes here a= input () b= input () c= (int) a + (int) b print (c) The input are numbers so thats the why I want to change them to int so it works as a calculator. This is a noob question but I have try many stuff and I don't work it out what am I missing? Is there a way to change the input to other variable than string? Maybe that will work better for my understanding if possible... Thanks a lot in advance for helping me understand

21st Nov 2020, 7:30 AM
Carlos Eduardo Marquez
Carlos Eduardo Marquez - avatar
7 Answers
+ 5
as Slick said you can do like this a=int(input()) here we are taking input as integer type. if we enter character then it will throw ValueError. also you can do it in your way a=input() b=input() here you are taking input as string type by default. if you enter a number then it also be considered as a string if you entered two numbers a=5 #string input() b=6 #string input() c=a+b then it will print outout : 56 to calculate it you have it convert string into integer using int() function a="5" b="6" c=int(a)+int(b) print(c) output : 11 you can check the type of any variable using type() >>> a=5 >>> type(a) <class 'int' > means #integer >>> a="5" >>> type(a) <class 'str'> #means string >>> a=1.5 >>> type(a) <class 'float'> # means float >>> a=[5, 6] >>type(a) <class 'list'> # means a list like this you can check the type of any object in python. Hope it helps!
21st Nov 2020, 7:57 AM
Ratnapal Shende
Ratnapal Shende - avatar
+ 17
I used map function. You can use any function like split,list comprehension, input for raw input. Follow the syntax,try to complete the course. Don't include arbitrary tags. All are learning. a,b = map(int,input().split()) print(a+b) Thanks
21st Nov 2020, 7:42 AM
Aditya
Aditya - avatar
+ 5
You might want to review this lesson. https://www.sololearn.com/learn/JUMP_LINK__&&__Python__&&__JUMP_LINK/4434/ Take a close look at the function call syntax.
21st Nov 2020, 7:41 AM
Kevin ★
+ 3
Carlos Eduardo Marquez you need use print() to see the output.. I shown example as interpreter >>> in interpreter we don't need to use the print.. here you can do like this : a=5 print(type(a))
22nd Nov 2020, 10:19 AM
Ratnapal Shende
Ratnapal Shende - avatar
+ 1
a = int(input()) b = int(input()) c = a+b print(c)
21st Nov 2020, 7:41 AM
Slick
Slick - avatar
0
Thank you all for taking the time :) Ratnapal Shende i tried to write: a=5 type(a) Just to check if it works. But got nothing on output, can you please tell me how to make this work? Thank you. That would be really helpful in the future i think. Kind regards
22nd Nov 2020, 5:50 AM
Carlos Eduardo Marquez
Carlos Eduardo Marquez - avatar
0
Ratnapal Shende thanks a lot, now is clear 😊.
22nd Nov 2020, 10:46 AM
Carlos Eduardo Marquez
Carlos Eduardo Marquez - avatar