Noah M.
asked 10/26/20python for everyone.
i did this
sum = 0
for i in range(0, 101):
if i%2 == 0:
sum = sum + i
but when i am trying to run it, it dont give me any answers. please help
1 Expert Answer
Rudy M. answered 10/26/20
Seasoned Tutor w/ upwards of 7 years experience
Be careful with how you tab your answer. The code you made should give you the correct answer so long as the shell can actually read your code.
format your answer like this:
sum = 0
for i in range(0, 101):
(tab) if i%2 == 0:
(tab) sum = sum + i
Be sure that the answer is tabulated properly (either press tab or press space 4 times in the areas where I placed tab in parenthesis).
Additionally you can answer the question more efficiently like this:
sum = 0
for i in range(0,101,2):
sum += i
The bold 2 represents the steps you want the iteration to take. In this case, because the step is 2, the loop increases by 2 with each pass. Using this method i will automatically be all even numbers.
sum += i essentially translates to sum = sum + i
it is just a shorter way of expressing it.
Hope this helps.
Still looking for help? Get the right answer, fast.
Get a free answer to a quick problem.
Most questions answered within 4 hours.
OR
Choose an expert and meet online. No packages or subscriptions, pay only for the time you need.
Noah M.
this is for my homework R4.2 Write a loop that computes a. The sum of all even numbers between 2 and 100 (inclusive). b. The sum of all squares between 1 and 100 (inclusive). c. The sum of all odd numbers between a and b (inclusive). d. The sum of all odd digits of n. (For example, if n is 32677, the sum would be 3 + 7 + 7 = 17.)10/26/20