sum_of_squares = sum(i ** 2) for i in range(1, 101)
I am attempting to square all numbers in a range and sum them up, but for whatever reason, when I run it, it gives me a syntax error pointing at for
.
I've seen examples almost completely identical to this, several times online, but I cannot figure out what the issue is.
I am having major difficulties with math involving lists/ranges (another example being unable to see if an integer is divisible by all the numbers in a list; it only divides by the first number in the list, constant int and operand errors). any sources online that may be helpful?
You're right to use a generator, but your syntax is wrong:
sum(i ** 2 for i in range(1, 101))
The generator is the argument to sum
. The above evaluates to 338350
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.