Python Forum
coding help lists, loops, and if statement
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
coding help lists, loops, and if statement
#1
My problem asks me to write a program to find all the divisors of a number. What I have done so far I realize is of target from that goal however there is part of this code that does not act as I want.

Inline 18 I have an if statement that is supposed to put x into a list if the check inline 17 is equal to 0. However, it only does that only 1 time after which the if statement lets through any number into the list decreasing by 1.

number = int(input("Enter a number for which you want to know all the divisors  "))
divisors = []
i = number
list = range(0, (number + 1))
check = list[number] % 2
if check != 0:
    print(str(number) + " does not have any divisors")
else:
    while i > 0:
        print("The list char")
        print(list[i])
        print("The i char")
        print(i)
        x = list[i] / 2
        print("The x value")
        print(x)
        check = list[i] % 2
        if check == 0:
            divisors.append(x)
        i -= 1
    print("The divisors of " + str(number) + " are: " + str(divisors))
Reply


Messages In This Thread
coding help lists, loops, and if statement - by ilondire05 - Jul-16-2019, 02:02 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Split dict of lists into smaller dicts of lists. pcs3rd 3 2,417 Sep-19-2020, 09:12 AM
Last Post: ibreeden
  sort lists of lists with multiple criteria: similar values need to be treated equal stillsen 2 3,313 Mar-20-2019, 08:01 PM
Last Post: stillsen
  Problems with For Loops and Lists Sagramor72 2 2,206 Mar-04-2019, 01:26 PM
Last Post: Sagramor72

Forum Jump:

User Panel Messages

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