-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProblem 2.py
More file actions
executable file
·27 lines (23 loc) · 849 Bytes
/
Problem 2.py
File metadata and controls
executable file
·27 lines (23 loc) · 849 Bytes
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
#!/usr/bin/env python
__author__ = 'Ned Udomkesmalee'
# By considering the terms in the Fibonacci sequence whose values do not exceed four million,
# find the sum of the even-valued terms.
def problem2():
print("Find the sum of the even-valued Fibonacci terms up to n.")
while True:
n = raw_input("What is n? ")
try:
n = int(n)
if n > 0:
fib_nums = [0, 1]
n = 1
while fib_nums[n] < n:
fib_nums += [fib_nums[n] + fib_nums[n-1]]
n += 1
print("Answer = %i" % sum([i for i in fib_nums[:-1] if i % 2 == 0]))
break
else:
print "*** n must be positive. ***"
except ValueError:
print "*** %s is not a valid integer. ***" % n
problem2()