-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexercise4task.txt
31 lines (23 loc) · 963 Bytes
/
exercise4task.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
Divisors
Exercise 4
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.)
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:
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
Happy coding!