Python Forum
New to python, having trouble with an exercise
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
New to python, having trouble with an exercise
#3
Mostly the errors are in the indentation - making the loops not end where they should, and not zeroing out values when they should. This works.
import math
def list_squared(m, n):
    final_list = []
    for number in range(m, n+1):
        divisors_list = []
        for i in range(1, number + 1):
            if number % i == 0:
                divisors_list.append(i)
        sum_of_divisors2 = 0
        for divisor in divisors_list:
            divisors_squared = divisor ** 2
            sum_of_divisors2 += divisors_squared
        if math.sqrt(sum_of_divisors2).is_integer() :
            final_list.append([number, sum_of_divisors2])
 
    return final_list
print(list_squared(5,1000))
Reply


Messages In This Thread
RE: New to python, having trouble with an exercise - by jefsummers - Feb-18-2020, 12:15 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Trouble with installing python domingo251 2 1,477 Sep-23-2023, 12:03 AM
Last Post: ICanIBB
  Headfirst Python Exercise GeorgeSears 3 2,437 Jun-11-2022, 06:36 PM
Last Post: GeorgeSears
  Wrong code in Python exercise MaartenRo 2 2,193 Jan-01-2022, 04:12 PM
Last Post: MaartenRo
  Python Exercise Doubt azure 4 3,718 Apr-21-2020, 01:15 PM
Last Post: azure
  More Python Embedding Trouble jibarra 3 3,984 Jul-11-2019, 09:25 PM
Last Post: Gribouillis
  Trouble installing Python 3.5 (.4 and .0) GKK 1 2,967 Aug-17-2018, 11:34 AM
Last Post: Larz60+
  Exercise 20, Learn Python 3 the Hard Way Dixon 6 8,291 Feb-26-2018, 04:54 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020