TestingDocs.com
Software Testing website
  • Automation
    • Selenium
    • JBehave Framework
  • Tutorials
    • MySQL Tutorials
    • Testlink
    • Maven
    • Git
  • IDEs
    • IntelliJ IDEA
    • Eclipse
  • Flowcharts
    • Flowgorithm
    • Raptor
  • About

Python

User Defined Functions is Python Program

Python Tutorials

Let’s write a Python program with a user-defined function to calculate the total and average rainfall of a year.

Python Program

#-----------------------------------------------
#Calculate total rainfall for the duration in months
#-----------------------------------------------
def total_rainfall(rainfall, durationInMonths):
 total = 0
 for i in range(durationInMonths):
 total +=rainfall[i] 
 return total


#-----------------------------------------------
#Calculate the average rainfall for a month
#-----------------------------------------------
def average_rainfall(rainfall, durationInMonths):
 return round(float(total_rainfall(rainfall, 
durationInMonths)/durationInMonths),3)

#-----------------------------------------------
# Main 
#-----------------------------------------------
def main():
 MONTHS = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jly', 'Aug', 'Sep',
 'Oct', 'Nov', 'Dec']
 durationInMonths=12
 rainfall = []
 
 for i in range(durationInMonths):
 rainfall.append(float(input("Enter the rainfall for " + MONTHS[i] 
+ " in cm:= ")))

 print("Total Rainfall :=" + str(total_rainfall(rainfall,durationInMonths))
 + " cm")
 print("Average Rainfall :=" + str(average_rainfall(rainfall,durationInMonths))
 + "cm")
 

main()

Screenshot

Python Functions

 

Output

Enter the rainfall for Jan in cm:= 0
Enter the rainfall for Feb in cm:= 0
Enter the rainfall for Mar in cm:= 0
Enter the rainfall for Apr in cm:= 0
Enter the rainfall for May in cm:= 0
Enter the rainfall for Jun in cm:= 5.5
Enter the rainfall for Jly in cm:= 12.8
Enter the rainfall for Aug in cm:= 19.5
Enter the rainfall for Sep in cm:= 22.0
Enter the rainfall for Oct in cm:= 0
Enter the rainfall for Nov in cm:= 0
Enter the rainfall for Dec in cm:= 0
Total Rainfall :=59.8 cm
Average Rainfall :=4.983 cm

Related Posts

Download Python Windows 11

Python /

Download & Install Python on Windows 11

PyCharm CE

Python /

Install PyCharm on Ubuntu Linux

Python Install on CentOS Linux

Python /

Install Python on CentOS

What is Python?

Python /

What is Python?

Install Python

Python /

Install Python 3.6 on Windows

‹ Python language bindings for Selenium› Writing a Function in Python Interactively

Recent Posts

  • MS Access Data Types
  • Install RAPTOR Avalonia on CentOS
  • Download RAPTOR Avalonia Edition on Windows
  • npm doctor command
  • Build & Run CLion Project
  • Create New CLion C Project on Windows
  • Configure CLion Toolchains on Windows
  • Launch CLion IDE on Windows
  • Activate CLion IDE
  • CLion IDE for C/C++ Development

Back to Top

Links

  • Contact
  • Privacy Policy
  • Cookie Policy

www.TestingDocs.com

Go to mobile version