From 6852cc41f42b276f358042ba8369ce51d97b757e Mon Sep 17 00:00:00 2001 From: HpEser Date: Fri, 27 May 2022 12:30:39 +0200 Subject: [PATCH] PyCoders Week3 Homework --- assignment_1.py | 61 +++++++++++++++++++++++++++++++++++++++++++++++++ assignment_2.py | 18 +++++++++++++++ assignment_3.py | 42 ++++++++++++++++++++++++++++++++++ assignment_4.py | 41 +++++++++++++++++++++++++++++++++ 4 files changed, 162 insertions(+) create mode 100644 assignment_1.py create mode 100644 assignment_2.py create mode 100644 assignment_3.py create mode 100644 assignment_4.py diff --git a/assignment_1.py b/assignment_1.py new file mode 100644 index 0000000..56efbcd --- /dev/null +++ b/assignment_1.py @@ -0,0 +1,61 @@ +# I. Write a function called int_return that takes an integer as input and returns the same integer. + +def int_return(n): + return n +print(int_return(5)) + + +# II. Write a function called add that takes any number as its input and returns that sum with 2 added. + +def add(n): + sum = n + 2 + return sum +print(add(5)) + + +# 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.) +name=input("Enter a name: ") +def greet(name): + print("Hello", name,"! Nice to meet you!") +greet(name) + + +# IV. Write a function, accum, that takes a list of integers as input and returns the sum of those integers. + +def accum(list): + sum = 0 + for i in list: + sum = sum + i + print("Sum is: ", sum) +list_1=[1,2,3,4,5,6,7,8,9,10] +accum(list_1) + +# 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”. + + +def length(list): + if len(list)>=5: + return "Longer than 5" + else: + return "Less than 5" + +list_1=["Tugrul", "Emrah","Ali","Emre"] +print(length(list_1)) + + +# 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. + +def diveded_by_2(n): + return n/2 + +def sum(n): + return n+6 + +n=int(input("Enter a n: ")) +print(sum(diveded_by_2(n))) + diff --git a/assignment_2.py b/assignment_2.py new file mode 100644 index 0000000..aa7e2a1 --- /dev/null +++ b/assignment_2.py @@ -0,0 +1,18 @@ +# 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. + +hour= float(input("Please enter total work hours: ")) +price_per_hour=float(input("Enter price for per hour: ")) +def computepay(hour, price_per_hour): + if hour<=40: + return hour*price_per_hour + return (40*price_per_hour)+((hour-40)*(1.5*price_per_hour)) + +print(computepay(hour,price_per_hour)) + + + diff --git a/assignment_3.py b/assignment_3.py new file mode 100644 index 0000000..cc8f34a --- /dev/null +++ b/assignment_3.py @@ -0,0 +1,42 @@ +# 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. +import math +r=float(input("Please enter a radius: ")) + +def sphere_area(r): + + A=math.pi*(r**2) + return A +print(sphere_area(r), "m2") + + +def sphere_volume(r): + v=(4/3)*r*sphere_area(r) + return v +print(sphere_volume(r), "cm3") + + +def sphere(r): + + def sphere_volume(r): + return + def sphere_area(r): + return + +sphere(r) + + + + + + + + + + + diff --git a/assignment_4.py b/assignment_4.py new file mode 100644 index 0000000..f0aa3a4 --- /dev/null +++ b/assignment_4.py @@ -0,0 +1,41 @@ +# 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. + + +def sum_of_numbers(start,end): + if start == end: + return end + elif startend: + return start + sum_of_numbers((start-1), 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. + + + + +def input_function(): + start = int(input("enter start: ")) + end = int(input("enter end: ")) + + result= sum_of_numbers(start,end) + print(result) + +input_function() +# 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. + +lst=[1,2,3,4,5,6,7,8,9] +def sum_of_numbers(lst): + total = sum(lst) + return total +print(sum_of_numbers(lst)) + + +