1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
amm1812
4 years ago
12

Write a function called sum_first_integers that takes a single positive integer parameter. The function should also return a pos

itive integer. In particular, it should return the sum of the positive integers from 1 to the value passed into the function. For example, if 5 is passed into the function, it should return 15 because that is the number you get when you add 1 + 2 + 3 + 4 + 5. Hint: We recommend using the range function and a for loop.
Computers and Technology
1 answer:
sashaice [31]4 years ago
6 0

Answer:

def sum_first_integers(n):

       if n <= 0 or type(n) != int:

               raise ValueError('Value must be a positive integer greater than 0')

       total = 0

       for num in range(1, n+1):

               total += num

       return total

Explanation:

The above function was written in Python, the first if statement is for validation purposes and it ensures that the argument passed is a positive integer. If it is not, a ValueError is raised

We leveraged on Python's range function, which accepts three arguments although we passed in two arguments. The first argument is where the range function will start from and the second is where it will end. If we want the range function to stop at value n, then the argument has to be n+1(This is because machine starts counting numbers from 0) and the last argument is the step in which the increment is done. If no value is passed to this argument, Python assumes the value to be 1 which is what is done in this case.

Hence range(1, 5) = (1,2,3,4) and range (2, 10, 2) = 2,4,6,8

You might be interested in
25 points select 3 options!!!!!!!!!!!!!!!!!!!!!!!!!
STALIN [3.7K]
A, B , and E sorry if I’m wrong
4 0
3 years ago
Explain why the control bus is not a bus.
slava [35]
Cause a control bus is a computer bus and controlled by a computer while a bus is controlled by a driver
3 0
3 years ago
Can anyone help me to write an example for html language​
ivann1987 [24]
No i cannot bcs i dont know
3 0
3 years ago
Read 2 more answers
Your southern region is not doing very well. You want to know why, so you ask for a special report showing the numbers for the s
Mila [183]

Answer:

D) Comparative

Explanation:

Comparative report is an analytical approach to report writing, where the aim is to determine the performance or contribution of individual constituents.

6 0
3 years ago
Design the logic for a program that outputs every number from 1 through 15.
ankoles [38]
Not sure what programming language, but i'll use Java

print (1)
print (2)
print (3)
print (4)
print (5)
print (6)
print (7)
print (8)
print (9)
print (10)
print (11)
print (12)
print (13)
print (14)
print (15)
3 0
3 years ago
Read 2 more answers
Other questions:
  • When desktop publishing software can interact with another software program, the two are said to
    6·1 answer
  • When at the rightmost cell in a row, press the ENTER key to move to the first cell in the next row; do not press the TAB key
    6·1 answer
  • Which color reflects more light red blue black or white
    6·1 answer
  • ____ is an approach to boundary spanning which results from using sophisticated software to search through large amounts of inte
    5·1 answer
  • Preventive Maintenance will will reduce potential hardware and Software Problems. Select two factors based on which Preventive m
    5·1 answer
  • Study the following two class interfaces: class Question { public: Question(); void set_text(string new_text); void set_answer(s
    11·1 answer
  • IF YOU LOVE GOD HELP ME......What should be covered in the conclusion of a presentation?
    11·2 answers
  • From which Menu option you can change page setup in MS Word​
    8·1 answer
  • Why would internet speed test be different on same network
    8·1 answer
  • Fill the labels!! Please quickly I need help quickly
    9·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!