+ 2

I want to convert mutiples strings values into integers values? How can I do it?

A,B,C,D =input("Enter values A,B,C,D separated by a comma: ").split(",") A=int(A) # These lines convert all the values into integers. I want to simplify them. B=int(B) C=int(C) D=int(D) print("Accepted values") if (A>0 and B>0 and C>0 and D>0 and A%2==0 and B>C and C>D and C+D > A+B) else print("Non-accepted values")

27th Sep 2020, 7:02 PM
Rosana Rodríguez Milanés
Rosana Rodríguez Milanés - avatar
4 Respuestas
+ 5
Instead working with 4 variables, an arry (list) should be used. with a comprehension the values can be gathered, splitted, converted to int and then stored in a list. After this you can process these numbers by using their indexes. arr = [int(num) for num in input('enter 4 numbers sep. by comma: ').split(',')] An other possible solution is using map as already mentioned, but it could be shortened like this: arr = list(map(int,input('enter 4 numbers sep. by comma: ').split(',')))
27th Sep 2020, 7:41 PM
Lothar
Lothar - avatar
0
Try using int()
27th Sep 2020, 7:10 PM
...
... - avatar
0
array = input().split(',') IntArray = list(map(lambda s: int(s), array)) A,B,C,D = intArray I think that's you needing for. I'm not sure about the assignment in final row, but it needs to be something similar. And look again the validations, you can simplify them. For example, you don't need B>0, because if C>0 and is B>C then the B will be bigger than zero.
27th Sep 2020, 7:14 PM
Антонио
Антонио - avatar