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
satela [25.4K]
3 years ago
9

Write a function in python that computes and returns the sum of the digits for any integer that is between 1 and 999, inclusive.

Use the following function header: def sum_digits(number): Once you define your function, run the following examples: print(sum_digits(5)) print(sum_digits(65)) print(sum_digits(658)) Note: Do not hard-code your function. Your function should run for any number between 1 and 999. Your function should be able to decide if the number has 1 digit, 2 digits, or 3 digits.
Computers and Technology
1 answer:
DanielleElmas [232]3 years ago
3 0

Answer:

def sum_digits(number):

   total = 0

   if 1 <= number <= 999:

       while number > 0:

           r = int (number % 10)

           total +=r

           number /= 10

   else:

       return -1

   return total

   

print(sum_digits(658))

Explanation:

Write a function named sum_digits that takes one parameter, number

Check if the number is between 1 and 999. If it is, create a while loop that iterates until number is greater than 0. Get the last digit of the number using mudulo and add it to the total. Then, divide the number by 10 to move to the next digit. This process will continue until the number is equal to 0.

If the number is not in the given range, return -1, indicating invalid range.

Call the function and print the result

You might be interested in
When you receive five job offers what does it mean
7nadin3 [17]

that you are a great canidate, in high demand, and well trained!

6 0
3 years ago
When a window is displayed in front of other windows, it's said to be
Dmitrij [34]
The active window

is the answer
7 0
3 years ago
How does an agile team obtain clarity on backing items that may be picked up in upcoming iterations? (1 correct answer)
Vinvika [58]

Answer:

The correct answer to the following question will be "Option 4".

Explanation:

Group seems to have a backlog optimization conversation with either the company owner after each iteration to obtain insight about the backlog things to have been collected in the future iterations or executions.

  • Without the need to get feedback on the backlog or pending things in preparation for potential implementations.
  • The next phase brings the dedicated backlog to completion. Iteration targets are calculated, based on dedicated research.
8 0
4 years ago
Which musical instrument would have the lowest pitch ?
Oduvanchick [21]

Answer: subcontrabass  tuba, then octocontra bass clarinet, then organ.

Explanation:

8 0
3 years ago
______ are special characters that allow you to
yKpoI14uk [10]

Answer:

Defined Expression

Explanation:

This will be your answer

7 0
3 years ago
Other questions:
  • Why should you log out when you finish an online session?
    9·1 answer
  • D. The 7-bit ASCII code for the character '&amp;' is:
    7·1 answer
  • An example of hardware is a _____. a)word processor b)database c)motherboard d)internet
    15·2 answers
  • Write a recursive function that takes a non-negative integer as an argument and displays the same number in reverse order (i.e.
    15·1 answer
  • In spreadsheet programs, labels and constant values are always copied exactly to the new location; what happens to formulas when
    10·1 answer
  • Write a method called sum with a while loop that adds up all numbers between two numbers a and b. The values for a and b can be
    11·2 answers
  • Which is the most efficient way to make the text in a row bold in every worksheet in the file?
    12·2 answers
  • 3.6 code practice (edhesive)
    8·1 answer
  • What is the last phase of the website development process?
    10·2 answers
  • Are they going to make season two of Midori Days
    15·2 answers
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!