Finding the Factors of a Number
00:01 Surprise! This lesson is not a lesson. It’s a challenge. Your challenge is to find the factors of a number. A factor is a positive number that can divide another number perfectly without any remainder. So for example, say you have 10.
00:16 5 is a factor of 10 because it divides perfectly. If you do 10 divided by 5, you get 2 without any remainder. 5 is a factor of 10. So is 2, because if you divide 10 by 2, you get 5 with no remainder.
00:32 So the challenge is for an input of any positive whole number—an integer—list all the factors of that number.
Martin Breuss RP Team on Nov. 22, 2023
@Steinar Martinussen Thanks for reporting the mismatched sample code download.
If you want a walkthrough to a possible solution, then you can check the section in the associated exercises course :)
doink on May 31, 2024
def factor_calculator(num):
internal_num = num
for factor in range(2, 10):
if internal_num % factor == 0:
return factor
else:
continue
return internal_num
number = input("Enter a number: ")
if number.isnumeric():
number = int(number)
if number <= 0:
print("Enter a number greater than 0 ")
else:
num_res = number
factors =[]
factor = factor_calculator(num_res)
while num_res != factor:
factors.append(factor)
num_res = int(num_res / factor)
factor = factor_calculator(num_res)
factors.append(factor)
if len(factors) == 1:
text = ", it's a prime number!"
else:
text = ""
print(f"The {number=} is divided into {factors=}{text}")
else:
print("Please, enter a valid number!")
Become a Member to join the conversation.
Steinar Martinussen on Nov. 18, 2023
This was a suprise challenge indeed.
Is the goal to solve this with just the knowledge from this course and its contents ? I might be missing some Python knowledge to make this happen.
The sample code attached to this course is also belonging to another course: Real Python: Functions and Loops course by Philipp Acsany