01Character Input <<
Previous Next >> 30 Pick Word
04 Divisors
Exercise 4 (and Solution)
Create a program that asks the user for a number and then prints out a list of all the divisors of that number. (If you don’t know what a divisor is, it is a number that divides evenly into another number. For example, 13 is a divisor of 26 because 26 / 13 has no remainder.)
創建一個程序,詢問用戶一個數字,然後打印出該數字的所有除數的列表。 (如果您不知道除數是多少,該數字將被除以另一個數。例如,13是26的除數,因為26/13沒有餘數。)
Discussion
The topics that you need for this exercise combine lists, conditionals, and user input. There is a new concept of creating lists.
本練習需要的主題包括列表,條件和用戶輸入。有一個創建列表的新概念。
There is an easy way to programmatically create lists of numbers in Python.
To create a list of numbers from 2 to 10, just use the following code:
有一種簡便的方法可以在Python中以編程方式創建數字列表。 要創建2到10的數字列表,只需使用以下代碼:
x = range(2, 11)
Then the variable x will contain the list [2, 3, 4, 5, 6, 7, 8, 9, 10]. Note that the second number in the range() function is not included in the original list.
Now that x is a list of numbers, the same for loop can be used with the list:
然後,變量x將包含列表[2、3、4、5、6、7、8、9、10]。請注意,range()函數中的第二個數字未包含在原始列表中。 既然x是一個數字列表,則該列表可以使用相同的for循環:
for elem in x:
print elem
Will yield the result:
將產生結果:
2
3
4
5
6
7
8
9
10
01Character Input <<
Previous Next >> 30 Pick Word