Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

PlentyOfCodes/Class6-Python-Module-Week3

Open more actions menu
 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
6 Commits
 
 

Repository files navigation

Class6-Python-Module-Week3

FENYX ACADEMY

PYTHON PROGRAMMING

FUNCTIONS ASSIGNMENT

1. QUESTION 1

  • i. Write a function called int_return that takes an integer as input and returns the same integer.
  • ii. Write a function called add that takes any number as its input and returns that sum with 2 added.
  • iii. Write a function called greet that takes any name (string), and greets the person. E.g. “Hello Bob! Nice to meet you!” (Only returns the greeting string, printing is done in the main program.)
  • iv. Write a function, accum, that takes a list of integers as input and returns the sum of those integers.
  • v. Write a function, length, that takes in a list as the input. If the length of the list is greater than or equal to 5, return “Longer than 5”. If the length is less than 5, return “Less than 5”.
  • vi. You will need to write two functions for this problem. The first function, divide that takes in any number and returns that same number divided by 2. The second function called sum should take any number, divide it by 2, and add 6. It should return this new number. You should call the divide function within the sum function. Do not worry about decimals.

2. QUESTION 2

  • i. Write a program to prompt the user for hours and rate per hour using input to compute gross pay. Pay should be the normal rate for hours up to 40 and time-and-a-half for the hourly rate for all hours worked above 40 hours. Put the logic to do the computation of pay in a function called computepay() and use the function to do the computation. The function should return a value. Use 45 hours and a rate of 10.50 per hour to test the program (the pay should be 498.75). You should use input to read a string and float() to convert the string to a number. Do not worry about error checking the user input unless you want to - you can assume the user types numbers properly.

3. QUESTION 3

  • i. Write a function sphere_volume that calculates and returns the volume of a sphere when given radius r as a parameter.
  • ii. Then write a main program that calls sphere_volume to print the volume of a sphere with a radius of 3.
  • iii. Write a function sphere that calls two other functions that calculate the area of a circle sphere_area and volume of a sphere sphere_volume for a given radius r as a parameter. Functions sphere_area and sphere_volume are nested inside the function sphere. Function sphere returns both the area and the volume of the sphere. (Reproduce sphere_volume function inside function sphere.) Make sure to print the results.
  • Formula to calculate the area of a circle: 𝑨 = 𝝅𝒓𝟐
  • Formula to calculate the volume of a circle: 𝑽 = 𝟒/𝟑 𝝅𝒓𝟑

4. QUESTION 4

  • i. Write a recursive function sum_of_numbers to calculate the sum of numbers within a given range that takes start and end as parameters. The function should work whether start is less or greater than end.
  • ii. Write a function called input_function to ask the user to enter the starting and ending numbers and call sum_of_numbers with those arguments and prints the result.
  • iii. Write the same function sum_of_numbers to calculate the sum of numbers but this time it takes a list of integers as an argument and calculates the sum of the integers in the list. Invoke the function in the main program.

Bonus Question 1

HACKERRANK: FIND DIGITS

Bonus Question 2

HACKERRANK: CAPITALIZE

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published
Morty Proxy This is a proxified and sanitized view of the page, visit original site.