|
1 | 1 | #!/usr/bin/python |
2 | 2 |
|
3 | 3 |
|
4 | | -user_number = raw_input('Which term do you want from the Fibonacci or Lucas sequence?\n') |
| 4 | +user_number = raw_input('Which term do you want from the Fibonacci or Lucas sequence?\n\n(be sure to ask for 0 if you want the first term from the sequence)\n\n') |
5 | 5 |
|
6 | 6 | user_number = int(user_number) |
| 7 | +print |
7 | 8 |
|
8 | 9 | def fibonacci(n): |
9 | 10 | """Return the nth value of the Fibonacci sequence of numbers""" |
@@ -33,24 +34,50 @@ def lucas(n): |
33 | 34 | #print fibonacci(user_number) |
34 | 35 |
|
35 | 36 | def sum_series(n, optional1 = 0, optional2 = 1): |
| 37 | + """This function takes one required argument and two optional ones. |
| 38 | +
|
| 39 | + If the optional arguments are 2,1, return nth Lucas number, otherwise, return nth Fibonacci number. If other arguments are given, return none until other series can be programmed. |
| 40 | + """ |
36 | 41 |
|
37 | 42 | if optional1 == 0 and optional2 == 1: |
38 | 43 | return fibonacci(n) |
39 | | - if optional1 == 2 and optional2 == 1: |
| 44 | + elif optional1 == 2 and optional2 == 1: |
40 | 45 | return lucas(n) |
| 46 | + else: |
| 47 | + "It looks like you want a different series" |
41 | 48 |
|
42 | 49 |
|
| 50 | +#Return Lucas value of user_number |
| 51 | +print 'This is the Lucas number corresponding to the term you gave\n' |
43 | 52 | print sum_series(user_number,2,1) |
44 | | - |
45 | | - # This is where I am going to put my tests and asserts |
| 53 | +print |
| 54 | +print 'This is the Fibonacci number corresponding to the term you gave\n' |
| 55 | +print sum_series(user_number,0,1) |
| 56 | +print |
46 | 57 |
|
47 | 58 | if __name__ == "__main__": |
48 | 59 |
|
| 60 | + #check to see if the Fibonacci numbers returned match the first 4 known values |
49 | 61 | assert fibonacci(0) == 0 |
50 | 62 | assert fibonacci(1) == 1 |
51 | 63 | assert fibonacci(3) == 2 |
52 | 64 | assert fibonacci(5) == 5 |
53 | 65 |
|
| 66 | + #check to see if the Lucas numbers returned match the first few known values (and the 6th for good measure) |
| 67 | + |
| 68 | + assert lucas(0) == 2 |
| 69 | + assert lucas(1) == 1 |
| 70 | + assert lucas(3) == 4 |
| 71 | + assert lucas(5) == 11 |
| 72 | + |
| 73 | + #make sure that the sum_series function performs Fibonacci as expected, and Lucas as expected when given the necessary arguments |
| 74 | + |
| 75 | + assert sum_series(0) == 0 |
| 76 | + assert sum_series(0,0,1) == 0 |
| 77 | + assert sum_series(5,0,1) == 5 |
| 78 | + assert sum_series(1,2,1) == 1 |
| 79 | + assert sum_series(5,2,1) == 11 |
| 80 | + assert sum_series(0) == 0 |
| 81 | + |
| 82 | + |
54 | 83 | print "\nAll Tests Pass!\n" |
55 | | - # # |
56 | | - # # |
|
0 commit comments