Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions Accumulation.py
Original file line number Diff line number Diff line change
@@ -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())
5 changes: 5 additions & 0 deletions Add2.py
Original file line number Diff line number Diff line change
@@ -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()
3 changes: 3 additions & 0 deletions AreaOfCircle.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
r = float(input('Enter the radius :'))
area= 3*r**2
print('Area of the Circle =', area)
15 changes: 15 additions & 0 deletions ComputePay.py
Original file line number Diff line number Diff line change
@@ -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)
10 changes: 10 additions & 0 deletions DivAddFunction.py
Original file line number Diff line number Diff line change
@@ -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()
6 changes: 6 additions & 0 deletions GreetingFunction.py
Original file line number Diff line number Diff line change
@@ -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'))
5 changes: 5 additions & 0 deletions IntegerReturn.py
Original file line number Diff line number Diff line change
@@ -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()
15 changes: 15 additions & 0 deletions LengthFunction.py
Original file line number Diff line number Diff line change
@@ -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())
23 changes: 23 additions & 0 deletions SphereVolume.py
Original file line number Diff line number Diff line change
@@ -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))




37 changes: 37 additions & 0 deletions SumOfRange.py
Original file line number Diff line number Diff line change
@@ -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))