+ 3

Why can't I use the input function in the def function in python?

this is the example that we are given: def print_sum_twice(x, y): print(x + y) print(x + y) print_sum_twice(5, 8) why can't I write it this way.. def print_sum_twice(): x = input("give the first number: ") y = input("give the 2nd number: ") print(x+y) print(x+y) print_sum_twice() it give me an error, why?

6th Jan 2018, 11:35 AM
Majd
Majd - avatar
2 Answers
+ 5
input is in string type so you must convert it to integer like: x = int(input())
6th Jan 2018, 11:43 AM
Vukan
Vukan - avatar
+ 4
In fact, what @Vukan told is true, but it stills should not gives an error as strings can be added. Did you put two lines as input ? If not, this is why it failed. Sololearn ask for ALL inputs at the beginning of the program
6th Jan 2018, 12:03 PM
Baptiste E. Prunier
Baptiste E. Prunier - avatar