From feb9308f424eb7a6660eb8f3dfa1259930b5a49d Mon Sep 17 00:00:00 2001 From: Serkan KLC Date: Sun, 12 Jun 2022 22:41:43 +0200 Subject: [PATCH] Python Module Week 3 Assignment Serkan --- Accumulation.py | 10 ++++++++++ Add2.py | 5 +++++ AreaOfCircle.py | 3 +++ ComputePay.py | 15 +++++++++++++++ DivAddFunction.py | 10 ++++++++++ GreetingFunction.py | 6 ++++++ IntegerReturn.py | 5 +++++ LengthFunction.py | 15 +++++++++++++++ SphereVolume.py | 23 +++++++++++++++++++++++ SumOfRange.py | 37 +++++++++++++++++++++++++++++++++++++ 10 files changed, 129 insertions(+) create mode 100644 Accumulation.py create mode 100644 Add2.py create mode 100644 AreaOfCircle.py create mode 100644 ComputePay.py create mode 100644 DivAddFunction.py create mode 100644 GreetingFunction.py create mode 100644 IntegerReturn.py create mode 100644 LengthFunction.py create mode 100644 SphereVolume.py create mode 100644 SumOfRange.py diff --git a/Accumulation.py b/Accumulation.py new file mode 100644 index 0000000..47a7bf0 --- /dev/null +++ b/Accumulation.py @@ -0,0 +1,10 @@ +#Write a function, accum, that takes a list of integers as input and returns the sum of those integers. +def accum(): + sum_integers = 0 + while True: + num = int(input('Please Enter a Number :')) + sum_integers = sum_integers+num + if num == 0: + break + return sum_integers +print(accum()) \ No newline at end of file diff --git a/Add2.py b/Add2.py new file mode 100644 index 0000000..97f8c64 --- /dev/null +++ b/Add2.py @@ -0,0 +1,5 @@ +# Write a function called add that takes any number as its input and returns that sum with 2 added +def add(): + num = int(input('Please Enter a Number :')) + return print(num+2) +add() \ No newline at end of file diff --git a/AreaOfCircle.py b/AreaOfCircle.py new file mode 100644 index 0000000..9c3e1fc --- /dev/null +++ b/AreaOfCircle.py @@ -0,0 +1,3 @@ +r = float(input('Enter the radius :')) +area= 3*r**2 +print('Area of the Circle =', area) diff --git a/ComputePay.py b/ComputePay.py new file mode 100644 index 0000000..d7f4884 --- /dev/null +++ b/ComputePay.py @@ -0,0 +1,15 @@ +# 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. +def computepay(h,r): + h = float(input('Please Enter Total Work Hour :')) + r = float(input('Please Enter Rate per Hour :')) + if h <= 40: + pay = h*r + else: + pay = 40*r+(h-40)*1.5*r + return print('The Pay should be :', pay) +computepay(45,10.5) \ No newline at end of file diff --git a/DivAddFunction.py b/DivAddFunction.py new file mode 100644 index 0000000..e88828b --- /dev/null +++ b/DivAddFunction.py @@ -0,0 +1,10 @@ +# 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. +def div(num): + return num/2 +def sum_div(): + num = float(input('Please Enter a Number :')) + return print(div(num)+6) +sum_div() diff --git a/GreetingFunction.py b/GreetingFunction.py new file mode 100644 index 0000000..d93d2c5 --- /dev/null +++ b/GreetingFunction.py @@ -0,0 +1,6 @@ +# 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.) +def greet(name): + name = input('Please Enter a Name : ') + return 'Hello {}! Nice to meet you'.format(name) +print(greet('name')) \ No newline at end of file diff --git a/IntegerReturn.py b/IntegerReturn.py new file mode 100644 index 0000000..3bb92be --- /dev/null +++ b/IntegerReturn.py @@ -0,0 +1,5 @@ +# Write a function called int_return that takes an integer as input and returns the same integer. +def int_return(): + num = int(input('Please Enter a Number :')) + return print(num) +int_return() diff --git a/LengthFunction.py b/LengthFunction.py new file mode 100644 index 0000000..6f93983 --- /dev/null +++ b/LengthFunction.py @@ -0,0 +1,15 @@ +# 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” +def length(): + a = [] + while True: + x = input('Write the elements of the list : ') + if x == 'exit': + break + a.append(x) + if len(a)>=5: + return 'Longer than 5' + else: + return 'Less than 5' +print(length()) diff --git a/SphereVolume.py b/SphereVolume.py new file mode 100644 index 0000000..89d4b74 --- /dev/null +++ b/SphereVolume.py @@ -0,0 +1,23 @@ +# Write a function sphere_volume that calculates and returns the volume of a sphere when given radius r as a parameter. +#def sphere_volume(r): + #pi = 3.14 + #return print('Volume is: ', (4*pi*r**3)/3) + +# 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. +def sphere_area(r): + pi = 3.14 + area = pi*r**2 + return area +def sphere_volume(r): + pi = 3.14 + volume = 4/3*sphere_area(r)*r + return volume +print('Volume is : ',sphere_volume(5)) +print('Area is :',sphere_area(5)) + + + + diff --git a/SumOfRange.py b/SumOfRange.py new file mode 100644 index 0000000..d723311 --- /dev/null +++ b/SumOfRange.py @@ -0,0 +1,37 @@ +# 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. + +def sum_of_numbers(x,y): + sum_range = 0 + if x>=y: + while x>=y: + sum_range = sum_range+x + x=x-1 + return (print(sum_range)) + elif y>x: + while y>=x: + sum_range = sum_range+y + y=y-1 + return (print(sum_range)) +# 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. +x = int(input('Please Enter the First Number :' )) +y = int(input('Please Enter the Second Number :' )) +sum_of_numbers(x,y) + +# 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. + +a = [] +while True: + x = int(input('Write the elements of the list : ')) + if x == 0: + break + a.append(x) + +print(sum(a)) + + + +