(NEEDS TO BE IN PYTHON 2.7) Part 1 Write a loop that sums all positive integers
ID: 3806969 • Letter: #
Question
(NEEDS TO BE IN PYTHON 2.7)
Part 1
Write a loop that sums all positive integers from 1 to 100
Note: The answer is 5150
Print out the result in sentence form such as:
The sum of all positive integers from 1 to 100 is 5150
Part 2
Make your loop interactive by allowing the user to choose the starting value and the ending value
Print out the results in sentence form
Hw7-Summing with a loop Part 1 Write a loop that sums all positive integers from 1 to 100 Note: The answer is 5150 Print out the result in sentence form such as: The sum of all positive integers from 1 to 100 is 5150 Part 2 Make your loop interactive by allowing the user to choose the starting value and the ending value Print out the results in sentence formExplanation / Answer
#python code
#part 1
sum = 0
i = 1
while i <= 100:
sum = sum + i
i =i + 1
print "Sum of all positive numbers from 1 to 100 is ",sum
#output: Sum of all positive numbers from 1 to 100 is 5050
#part 2
startingValue = int(raw_input("Enter the starting value: "))
endingValue = int(raw_input("Enter the ending value: "))
sum = 0
while startingValue <= endingValue:
sum = sum + startingValue
startingValue = startingValue + 1
print "Sum of all positive numberss form",startingValue,"to",endingValue,"is",sum
'''
output:
Enter the starting value: 23
Enter the ending value: 56
Sum of all positive numberss form 57 to 56 is 1343
'''
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.