简体   繁体   中英

Python factors of a number

Prompt the user to input a number. Print out all factors of that number.

number = (input("Enter a number: "))

string = ""

for i in range(2, int(number)):

    if int(number) / i == 0:

        string = string + str(i)

print(string)

Why is this not returning anything?

You have to replace if int(number) / i == 0: with if int(number) % i == 0: , as you're getting the quotient, not the remainder

Try this:

number = int(input("Enter a number: "))
string = ' '.join([str(i) for i in range(2, number + 1) if number % i == 0])

print(string)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM