Locked learning resources

Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Finding the Factors of a Number

Avatar image for Steinar Martinussen

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

Avatar image for Martin Breuss

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 :)

Avatar image for doink

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.