PYTHON HELP! PLEASE HELP ME FIX THIS CODE! The assignment is posted as well. 2 #
ID: 3804569 • Letter: P
Question
PYTHON HELP! PLEASE HELP ME FIX THIS CODE!
The assignment is posted as well.
2 #Find the sum of all even numbers between 0 and 10 (inclusive) 3 def sumofAllEvenNumbersBetweenzeroAndTen sum 0 5 #Iterated through all even numbers between 0 and 10 using range method 6 #For the third parameter of range used step to jump over items. 7 #Add num to sum 8 sum num 9 print"Sum of all even numbers between 0 and 10 (inclusive) is %d" (sum) 10 11 #Find all powers of 2 from 0 to 16 12 def find Powers0fTwoFromZeroToSixteen(): print'' 0 power of 2 is 1" 13 14 #Iterated through the range of numbers 15 v for num in range 1,17 16 #Make an expression string like 2*2 using string multiply function ("a" x 3 "aaa") 17 #Strip out the leading '*'s and newlines from the expression string expression string ("2*"num0. rstrip strip (os.linesp) print''%d power of 2 is %d" num, eval(expression string) 18 19 20 #Function that calls itself 21 v def factorial (num) if num 0 or num 1: 22 return 1 23 return num factorial (num 1) 24 25 26 #Find factorial of all numbers between 1 and 10 (inclusive) 27 I could not use range method her so I used 11. 28 v def factoria10fAllNumbersBetweenoneAndTen for num in range 1,11 29 print Factorial of sod is sed" num, factorial(num) 30 31 32 sumofALLEvenNumbersBetweenzeroAndTen 33 find All Powers0fTwoFromZero ToSixteen 34 factorial0fAllNumbersBetweenoneAndTen 35Explanation / Answer
Please add the following codes in the functions :
1. sum of even numbers :
sum=0
while i<=10:
if i%2==0:
sum=sum+i
i=i+1
print 'Sum of even numbers',sum
2. Power of 2
terms = 16
# Uncomment to take number of terms from user
#terms = int(input("How many terms? "))
# use anonymous function
result = list(map(lambda x: 2 ** x, range(terms)))
# display the result
print("The total terms is:",terms)
for i in range(terms):
print("2 raised to power",i,"is",result[i])
3. Factorial :
number = 0;
while number <=10:
if number == 0:
print("Factorial of 0 is 1 ")
elif number < 0:
print("Factorial of negative numbers doesn't exist..!! ")
else:
fact = 1
for i in range(1, number+1):
fact = fact*i
print("Factorial of", number, "is", fact, " ")
number = number + 1
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.