Hướng dẫn how do you print the factors of a number in python for loop? - làm thế nào để bạn in các yếu tố của một số trong python cho vòng lặp?

Mã nguồn

# Python Program to find the factors of a number

# This function computes the factor of the argument passed
def print_factors(x):
   print("The factors of",x,"are:")
   for i in range(1, x + 1):
       if x % i == 0:
           print(i)

num = 320

print_factors(num)

Đầu ra

The factors of 320 are:
1
2
4
5
8
10
16
20
32
40
64
80
160
320

Lưu ý: Để tìm các yếu tố của một số khác, hãy thay đổi giá trị của num. To find the factors of another number, change the value of num.

Trong chương trình này, số có hệ số sẽ được tìm thấy được lưu trữ trong num, được chuyển đến hàm

The factors of 320 are:
1
2
4
5
8
10
16
20
32
40
64
80
160
320
1. Giá trị này được gán cho biến X trong
The factors of 320 are:
1
2
4
5
8
10
16
20
32
40
64
80
160
320
1.

Trong hàm, chúng tôi sử dụng vòng

The factors of 320 are:
1
2
4
5
8
10
16
20
32
40
64
80
160
320
3 để lặp lại từ i bằng x. Nếu x hoàn toàn chia hết bởi tôi, thì đó là yếu tố của x.

Trong bài viết này, bạn sẽ học cách tìm các yếu tố của một số sử dụng cho vòng lặp và trong khi vòng lặp trong ngôn ngữ lập trình Python.

Ví dụ

Đầu vào: 60

Các yếu tố của & nbsp; 60 là: 1 2 3 4 5 6 10 12 15 20 30 60 60

Đầu vào: 70

Các yếu tố của & nbsp; 70 là: 1 2 5 7 10 14 35 70

Các yếu tố của một số là gì?

Các yếu tố của một số được định nghĩa là các số chia số ban đầu mà không để lại bất kỳ phần còn lại (nhắc lại trái = 0).(left reminder = 0).

Bạn nên có kiến ​​thức về các chủ đề sau trong chương trình Python để hiểu các chương trình này:

  • Hàm Python
    The factors of 320 are:
    1
    2
    4
    5
    8
    10
    16
    20
    32
    40
    64
    80
    160
    320
    
    4
  • Hàm Python
    The factors of 320 are:
    1
    2
    4
    5
    8
    10
    16
    20
    32
    40
    64
    80
    160
    320
    
    5
  • Python
    The factors of 320 are:
    1
    2
    4
    5
    8
    10
    16
    20
    32
    40
    64
    80
    160
    320
    
    6 Câu lệnh LOOP
  • Python
    The factors of 320 are:
    1
    2
    4
    5
    8
    10
    16
    20
    32
    40
    64
    80
    160
    320
    
    7 Câu lệnh LOOP
  • Tuyên bố Python
    The factors of 320 are:
    1
    2
    4
    5
    8
    10
    16
    20
    32
    40
    64
    80
    160
    320
    
    8
  • Hàm Python
    The factors of 320 are:
    1
    2
    4
    5
    8
    10
    16
    20
    32
    40
    64
    80
    160
    320
    
    9

1. & NBSP; Chương trình Python để tìm các yếu tố của một số sử dụng cho vòng lặp

# Python program to find factors of a number using for loop

print ("Enter the positive integer number: ", end="")
random_number = int (input ())

print ("The factors of the ", random_number, "are: ", end="")
for i in range (1, random_number + 1):
    if random_number % i == 0:
        print (i, end=" ")
print (end="\n")

Chạy chương trình

Đầu ra

# Python program to find factors of a number using for loop

print ("Enter the positive integer number: ", end="")
random_number = int (input ())

print ("The factors of the ", random_number, "are: ", end="")
for i in range (1, random_number + 1):
    if random_number % i == 0:
        print (i, end=" ")
print (end="\n")
0

2. & NBSP; Chương trình Python để tìm các yếu tố của một số sử dụng trong khi vòng lặp

# Python program to find factors of a number using while loop

print ("Enter the positive integer number: ", end="")
random_number, i = int (input ()), 1

print ("The factors of the ", random_number, "are: ", end="")
while i <= random_number:
    if random_number % i == 0:
        print (i, end=" ")
    i += 1
print (end="\n")

Chạy chương trình

Đầu ra

# Python program to find factors of a number using for loop

print ("Enter the positive integer number: ", end="")
random_number = int (input ())

print ("The factors of the ", random_number, "are: ", end="")
for i in range (1, random_number + 1):
    if random_number % i == 0:
        print (i, end=" ")
print (end="\n")
1

2. & NBSP; Chương trình Python để tìm các yếu tố của một số sử dụng trong khi vòng lặp

Giải trình

Với nó tự nhắc nhở bằng không để tìm các yếu tố có thể của số ngẫu nhiên này.

Tính toán tương tự được áp dụng cho chương trình thứ hai với vòng lặp

The factors of 320 are:
1
2
4
5
8
10
16
20
32
40
64
80
160
320
7.

Tôi đang cố gắng in các yếu tố của số 20 trong Python vì vậy nó đi: 20 10 5 4 2 1

Tôi nhận ra đây là một câu hỏi khá đơn giản, nhưng tôi chỉ có một câu hỏi về một số chi tiết cụ thể trong nỗ lực của tôi. Nếu tôi nói:

def factors(n):
    i = n
    while i < 0:
        if n % i == 0:
            print(i)
        i-= 1

Khi tôi làm điều này, nó chỉ in ra 20. Tôi nghĩ rằng có điều gì đó không ổn khi tôi gán i = n và sau đó giảm tôi, nó cũng ảnh hưởng đến n? Làm thế nào mà nó hoạt động? Ngoài ra tôi nhận ra điều này có thể được thực hiện với một vòng lặp cho một vòng Làm điều này bằng cách sử dụng chỉ lặp. Cứu giúp?

Lưu ý: Đây không phải là một câu hỏi bài tập về nhà mà tôi đang cố gắng tự mình học lại Python vì đã được một thời gian nên tôi cảm thấy khá ngớ ngẩn khi bị mắc kẹt trong câu hỏi này :(

Hướng dẫn how do you print the factors of a number in python for loop? - làm thế nào để bạn in các yếu tố của một số trong python cho vòng lặp?

Sẽ

10,9k9 huy hiệu vàng68 Huy hiệu bạc74 Huy hiệu đồng9 gold badges68 silver badges74 bronze badges

Đã hỏi ngày 7 tháng 7 năm 2014 lúc 22:05Jul 7, 2014 at 22:05

3

while i < 0:

Điều này sẽ sai ngay từ đầu, vì

# Python program to find factors of a number using for loop

print ("Enter the positive integer number: ", end="")
random_number = int (input ())

print ("The factors of the ", random_number, "are: ", end="")
for i in range (1, random_number + 1):
    if random_number % i == 0:
        print (i, end=" ")
print (end="\n")
5 bắt đầu tích cực, có lẽ. Bạn muốn:

while i > 0:

Trong lời nói, bạn muốn "bắt đầu

# Python program to find factors of a number using for loop

print ("Enter the positive integer number: ", end="")
random_number = int (input ())

print ("The factors of the ", random_number, "are: ", end="")
for i in range (1, random_number + 1):
    if random_number % i == 0:
        print (i, end=" ")
print (end="\n")
5 TẮT tại
# Python program to find factors of a number using for loop

print ("Enter the positive integer number: ", end="")
random_number = int (input ())

print ("The factors of the ", random_number, "are: ", end="")
for i in range (1, random_number + 1):
    if random_number % i == 0:
        print (i, end=" ")
print (end="\n")
7 và giảm nó trong khi nó vẫn còn lớn hơn 0, kiểm tra các yếu tố ở mỗi bước".


>>> def factors(n):
...     i = n
...     while i > 0:  # <--
...         if n % i == 0:
...             print(i)
...         i-= 1
... 
>>> factors(20)
20
10
5
4
2
1

Đã trả lời ngày 7 tháng 7 năm 2014 lúc 22:08Jul 7, 2014 at 22:08

Hướng dẫn how do you print the factors of a number in python for loop? - làm thế nào để bạn in các yếu tố của một số trong python cho vòng lặp?

Arshajiiarshajiiarshajii

125K24 Huy hiệu vàng234 Huy hiệu bạc282 Huy hiệu Đồng24 gold badges234 silver badges282 bronze badges

6

Điều kiện trong khi phải là

# Python program to find factors of a number using for loop

print ("Enter the positive integer number: ", end="")
random_number = int (input ())

print ("The factors of the ", random_number, "are: ", end="")
for i in range (1, random_number + 1):
    if random_number % i == 0:
        print (i, end=" ")
print (end="\n")
8 chứ không phải
# Python program to find factors of a number using for loop

print ("Enter the positive integer number: ", end="")
random_number = int (input ())

print ("The factors of the ", random_number, "are: ", end="")
for i in range (1, random_number + 1):
    if random_number % i == 0:
        print (i, end=" ")
print (end="\n")
9 vì nó sẽ không bao giờ thỏa mãn nó khi tôi bắt đầu trong 20 (hoặc nhiều trường hợp khác)

Đã trả lời ngày 7 tháng 7 năm 2014 lúc 22:07Jul 7, 2014 at 22:07

Hướng dẫn how do you print the factors of a number in python for loop? - làm thế nào để bạn in các yếu tố của một số trong python cho vòng lặp?

0

Hy vọng câu trả lời của tôi sẽ giúp!

#The "while True" program allows Python to reject any string or characters
while True:
try:
    num = int(input("Enter a number and I'll test it for a prime value: "))
except ValueError:
    print("Sorry, I didn't get that.")
    continue
else:
    break

#The factor of any number contains 1 so 1 is in the list by default.
fact = [1]

#since y is 0 and the next possible factor is 2, x will start from 2.
#num % x allows Python to see if the number is divisible by x
for y in range(num):
    x = y + 2
    if num % x is 0:
        fact.append(x)
#Lastly you can choose to print the list
print("The factors of %s are %s" % (num, fact))

Đã trả lời ngày 10 tháng 6 năm 2019 lúc 6:29Jun 10, 2019 at 6:29

Hướng dẫn how do you print the factors of a number in python for loop? - làm thế nào để bạn in các yếu tố của một số trong python cho vòng lặp?