﻿ fred

fred

```#  Answers to Python Exerecises #2

def sum_squares(low,high):
i = low
subtotal = 0
while i <= high:
subtotal = subtotal + i**2
i = i + 1
return subtotal

print('sum_squares(1,3) should be 14 and is',sum_squares(1,3))

def sum_powers(low,high,power):
i = low
subtotal = 0
while i <= high:
subtotal = subtotal + i**power
i = i + 1
return subtotal

print('sum_powers(1,4,3) should be 100 and is', sum_powers(1,4,3))

def SUM_POWERS(a,b,power):
if a <= b:
return sum_powers(a,b,power)
return sum_powers(b,a,power)

print('SUM_POWERS(4,1,3) should be 100 and is',SUM_POWERS(4,1,3))

def fizzbuzz(n):
i = 1
while i <= n:
if i % 15 == 0:
print('fizzbuzz')
elif i % 3 == 0:
print('fizz')
elif i % 5 == 0:
print('buzz')
else:
print(i)
i = i + 1

#print(fizzbuzz(16))

def num_digits(n):
ndigs = 1
while n >= 10:
n = n // 10
ndigs = ndigs + 1
return ndigs

print('num_digits(5407) should be 4 and is',num_digits(5407))

def bin_to_dec(n):
placevalue = 1
total = 0
while n > 0:
if n % 10 == 1:
total = total + placevalue
n = n // 10
placevalue = placevalue * 2