Check Primality Functions (檢查功能)11 <<
Previous Next >> File Overlap Solutions(文件重疊)23
Divisors(除數)4
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.
(本練習需要的主題包括列表,條件和用戶輸入。有一個創建列表的新概念。有一種簡便的方法可以在Python中以編程方式創建數字列表。)
To create a list of numbers from 2 to 10, just use the following code:
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:
for elem in x:
print elem
Will yield the result:
2.3.4.5.6.7.8.9.10
Exercise 4(練習4)
Create a program that asks the user for a number and then prints out a list of all the divisors of that number. (創建一個程序,詢問用戶一個數字,然後打印出該數字的所有除數的列表。)
solution(解答)
num=int(input("Enter the number you want to divide: "))
listRange=list(range(1,num+1))
divisorList=[]
for number in listRange:
if num % number == 0:
divisorList.append(number)
print(divisorList)
Check Primality Functions (檢查功能)11 <<
Previous Next >> File Overlap Solutions(文件重疊)23