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
What is a moves track
saveliy_v [14]
It is an activity diary
8 0
4 years ago
What's the function of a cyclebin​
amid [387]

Answer:

In Windows, the Recycle Bin is a folder or directory where deleted items are temporarily stored. Deleted files are not permanently removed from the hard drive but are sent instead to the Recycle Bin, unless they are too large.

3 0
3 years ago
Identify disadvantages that web designers encountered when using tables for page layout. Check all of the boxes that apply.
11Alexandr11 [23.1K]

Answer:

A and C

Explanation:

I got the question correct on Edge.

7 0
3 years ago
Read 2 more answers
Which of these has led to the evolution of mobile marketing?
Nana76 [90]
D. using cell phones in a work place. when cell phones came out with apps and games people started paying more attention to the social media apps and their games then their own job, hope this helps :)
5 0
3 years ago
Stanford Rosenberg Computing wants to establish an assembly line for producing a new product, the Personal Digital Assistant (PD
Elina [12.6K]

Answer:

d what is the efficiency of this assembly line assignment?

8 0
4 years ago
Other questions:
  • Do all accounts use the same routing number
    12·1 answer
  • Nearly all states allow citizens who are not able to go to the polls due to business travel to vote using this method: Internet
    9·2 answers
  • You are asked to add speech to a robotic receptionist that sits at the front desk of a large organization. The robot has an anim
    10·2 answers
  • To generate a report with exact results based on specific criteria it is best to base the report on a(n) ____________________ cr
    6·1 answer
  • Write a function isRed() that accepts a string parameter and looks for the presence of the word ‘red’ in the string. If it is fo
    5·2 answers
  • Discuss your favorite video games and why you like them. What types of features make a game fun to play? Why are they features e
    11·2 answers
  • 75 pts. Conduct online research about 10 of the most famous and dangerous computer viruses that threatened computer networks aro
    13·1 answer
  • In excel online what is it called when you join connecting cells together to span across more than one column and/or row?
    7·2 answers
  • Text me on instagram forever.brazy so we can watch a movie​
    15·1 answer
  • Select all the correct answers. Which two statements are true about electric current through a circuit? Current flows from a pos
    14·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!