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 new ransomware program that encrypts your personal files and demands payment for the files' decryption keys? Multiple
frosja888 [35]

Answer: Simple-locker

Explanation:Simple-locker is the program that works on the technique in which it automatically encrypts the data or files and then demand a certain ransom or money from the user for the decryption of that data. It works on the function to gain the ransom or incentive in the financial form.

The decryption can only be carried out in safe way when the victim has the key to decrypted data or file.

5 0
3 years ago
Write a program to input elements of 4*3matrix and prints its elements properly using array ​
Harlamova29_29 [7]

Answer:

Step by step explanation:

7 0
1 year ago
Select the correct answer from each drop-down menu.
lys-0071 [83]

Answer: A and C

Explanation:I did it before

6 0
3 years ago
What type of malicious software tries to gather information about you without your consent?
AlekseyPX

Answer:

B-malware

Explanation:

I do tech and i help work on computars.

6 0
3 years ago
Assume that a gallon of paint covers about 350 square feet of wall space. Create an application with a main() method that prompt
Makovka662 [10]

Answer:

<em>C++</em>

#include <iostream>

#include <cmath>

using namespace std;

///////////////////////////////////////////////////////////////////////

int noOfGallons(int wallArea) {

   int gallon = 350;

   int gallon_price = 32.0;

   float total_price = 0.0;

   ///////////////////////////////////////////

   float noOfGallons = (float)wallArea/(float)gallon;

   printf("No of gallons needed %.2f", noOfGallons);

   cout<<endl;

   //////////////////////////////////////////

  total_price = noOfGallons*gallon_price;

   return round(total_price);

}

int wallArea(int length, int width, int height) {

   int wall_area = length*width;

   int total_price = noOfGallons(wall_area);

}

///////////////////////////////////////////////////////////////////////

int main() {

   int length, width, height;

   ///////////////////////////////////////////

   cout<<"Enter length: ";

   cin>>length;

   

   cout<<"Enter width: ";

   cin>>width;

   

   cout<<"Enter height: ";

   cin>>height;

   

   cout<<endl;

   ///////////////////////////////////////////

   int total_price = wallArea(length, width, height);

  cout<<"Total price: $"<<total_price;

   ///////////////////////////////////////////

   return 0;

}

5 0
2 years ago
Other questions:
  • Header and Footer options are located in the _____ tab.
    7·1 answer
  • Interest assessments are always accurate. True False
    5·1 answer
  • ____ is a bundled set of communications services, including internet, telephone, and tv, that operates over a total fiber-optic
    5·1 answer
  • Window frame will expand to fill the entire desktop when you
    12·1 answer
  • 3.19 LAB: Seasons In C++ Write a program that takes a date as input and outputs the date's season. The input is a string to repr
    15·1 answer
  • To simplify the conceptual design, most higher-order relationships are decomposed into appropriate equivalent _____ relationship
    11·1 answer
  • I need help 50 points and brainiest if you answer
    10·2 answers
  • What does input allow a computer to do
    14·1 answer
  • Capstone Project part 11 quiz
    6·1 answer
  • What is a port?
    14·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!