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
Give a pseudo-code description of the O(n)-time algorithm for computing the power function p(x,n).Also draw the trace of computi
laila [671]

Answer:

p(x,n)

1. if(n==0)    [if power is 0]

2.    then result =1.

3.else

4. {    result=1.

5.      for i=1 to n.

6.          {  result = result * x. }  [each time we multiply x once]

7.       return result.

8.  }

Let's count p(3,3)

3\neq0, so come to else part.

i=1: result = result *3 = 3

i=2: result = result *3 = 9

i=2: result = result *3 = 27

Explanation:

here the for loop at step 4 takes O(n) time and other steps take constant time. So overall time complexity = O(n)

6 0
3 years ago
Stamps offers at-home postage and mailing for a monthly fee. Customers are able to carry a balance on their accounts that allows
alexdok [17]

Answer:

The pop-up button is necessary to show acceptance of the terms

Explanation:

5 0
2 years ago
Are sql injections legal?
natima [27]

Essentially, if you are seen to be someone who knows what you are doing, then even typing in a single-quote to a web form has been enough to be arrested and charged over in the past.

But lets say i'm writing a pen test tool that will be doing sqli testing and let it loose on sites that are 'out in the wild'. I'm not going to be doing dumps of any information. But is just the vulnerability scan itself illegal?



8 0
3 years ago
What two tabs does Outlook have that PowerPoint does not? View and Send/Receive Folder and Home Folder and Home Send/Receive and
grin007 [14]
<h2>What are the ribbon tabs in Microsoft Outlook?</h2>

The main Outlook menu has a ribbon tab with default commands. File, Home, Send/Receive, Folder, and View are commands on the main ribbon tab. The Backstage view is where you can see information and options pertaining to the user account and settings.

3 0
2 years ago
What did Stalin hope the Western powers would do because of the blockade?​
Elodia [21]

Answer:

Stalin's plan was to cut western Germany off from its capital

Explanation:

Stalin's plan was to cut western Germany off from its capital so that the new government, based in Berlin, could not control its territory in western Germany. He hoped that this would prove that a divided Germany would not work in practice.

6 0
3 years ago
Other questions:
  • Help with this robotics hw pls
    5·1 answer
  • Show the stack with all activation record instances, including static and dynamic chains, when execution reaches position 1 in t
    6·1 answer
  • Select the correct answer from each drop-down menu.
    6·1 answer
  • 7.2 need help plzs 15 points
    13·1 answer
  • By convention only, either the first usable address or the last usable address in a network is assigned to the router (gateway)
    11·1 answer
  • Imagine that you are preparing a multimedia presentation. What are the four things you need to consider when getting started?
    14·2 answers
  • A student can improve performance by decreasing
    14·1 answer
  • A(n) _____ is a computerized system by which subscribers are able to communicate to all other subscribers by sending a transmiss
    5·1 answer
  • after adding a sound to a slide, the audio tools tab will allow you to apply artistic effects and quick styles to your sound ico
    7·1 answer
  • Why is technology bad for you
    9·2 answers
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!