This works:
s = "Arkansas 40\n Washington 83\n North Dakota 49\n New Jersey 24"
sum(int(n) for n in s.split() if n.isdigit())
196
Basically the .isdigit() method does the trick here. No need for a try/except
as if there are no numbers in the string, no n
will meet the isdigit()
condition, and sum
will return 0.