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
Given a sorted list of integers, output the middle integer. assume the number of integers is always odd.
Svetllana [295]

integer userInput

integer i

integer mid

integer array(20) number

userInput = 1

for i = 0; userInput >= 0; i = i + 1

  if number[i] > -1

     userInput = Get next input

     number[i] = userInput

i = i - 1

mid = i / 2

if i > 9

  Put "Too many inputs" to output

elseif i % 2 == 0

  Put number[mid - 1] to output

else

  Put number[mid] to output

4 0
2 years ago
Self-disclosure is most likely to occur _____________.a.
Feliz [49]

The answer is..

C.  During one-on-one conversations

4 0
3 years ago
Read 2 more answers
Which type of word processing programs enables users to include illustrations within the program?
Nookie1986 [14]
Microsoft Word, Libre, and Google Docs have always enabled me to insert illustrations. Look for an image icon in the task icon, here you can select an image from your files. 
4 0
3 years ago
After making a PowerPoint presentation about a new line of clothing designs, Henri notices that he used the word “gorgeous” on n
sineoko [7]

The spell checker and thesaurus are not underneath the view tab, so 1 and 2 are wrong.

If you replace every instance of the word gorgeous, then you would not be adding variety. So d is incorrect.

Therefore:

The only correct answer is C, as it is correct navigation and it adds word variety.

8 0
4 years ago
What are some examples for Environmental Technology? Also some negative and positive effects?
lakkis [162]
Wave energy and wind power (also hydroelectric dams as well), they create energy without pollution but need a lot of maintenance and some people disagree with their aesthetic influence of the countryside as they are very industrial looking  <span />
5 0
3 years ago
Other questions:
  • Mary is an aspiring web designer. Which degree she should opt for to strengthen her portfolio? A. bachelor’s degree in computer
    11·1 answer
  • Caitlyn's Crafty Creations computes a retail price for each product as the cost of materials plus $14 multiplied by the number o
    10·1 answer
  • Computer crimes are a big concern since the creation of innovative technological advances; which is/are examples of computer cri
    13·2 answers
  • As a sports store owner, the higher the price you charge for gear...
    8·2 answers
  • Why is internet download speed slower on a computer vs phone
    9·1 answer
  • Consider that a man is watching a picture of a Black Horse. He receives an external stimulus from input channel. Man gains some
    14·1 answer
  • If the bookstore sold 8 books for $66 at that rate how much was one book
    10·2 answers
  • Explain how information in an access log could be used to identify the true identity of an impostor who has acquired unauthorize
    13·1 answer
  • What are the key constructs of a G&amp;T Value Delivery objective?
    6·1 answer
  • What is the current situation of drone technology in emergency rescue and recovery
    14·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!