بخشی از مقاله

Several example codes

~1 دقیقه مطالعه · آخرین به‌روزرسانی ۱۹ مهر ۱۴۰۴

Program Overview

This Python program reads the volume of water consumed (in cubic meters) and the type of usage—residential, commercial, or industrial.
Based on the input, it calculates the total payable amount according to the following rules:


Pricing Rules:

  • Residential:
    • If volume < 80 → 2500 Rials per m³
    • If 80 ≤ volume ≤ 120 → 3000 Rials per m³
    • If volume > 120 → 3500 Rials per m³
  • Commercial:
    • If volume < 150 → 4000 Rials per m³
    • If volume ≥ 150 → 4500 Rials per m³
  • Industrial:
    • Flat rate → 5000 Rials per m³

Python Code:


def calculate_water_bill(volume, usage_type):
    if usage_type == "residential":
        if volume < 80:
            rate = 2500
        elif volume <= 120:
            rate = 3000
        else:
            rate = 3500
    elif usage_type == "commercial":
        if volume < 150:
            rate = 4000
        else:
            rate = 4500
    elif usage_type == "industrial":
        rate = 5000
    else:
        print("Invalid usage type.")
        return None

    total = volume * rate
    return total

# Run the program
volume = float(input("Enter water consumption (m³): "))
usage_type = input("Enter usage type (residential, commercial, industrial): ")

bill = calculate_water_bill(volume, usage_type)
if bill is not None:
    print(f"Total payable amount: {bill:.0f} Rials")

Sample Output (input: 100 m³, usage type: residential):


Total payable amount: 300000 Rials

Step-by-Step Explanation:

- The program reads the water volume and usage type from the user
- It applies the correct rate based on the usage category and volume
- Residential rates are tiered; commercial rates are split into two bands; industrial has a flat rate
- The final bill is calculated and displayed in Rials


نوشته و پژوهش‌شده توسط دکتر شاهین صیامی

مقالات مرتبط

Complete Guide to Python Decorators – Enhancing Function Behavior with Reusable Logic

Decorators in Python are a powerful tool for modifying or extending the behavior of functions and classes without changing their original code. They allow developers to implement reusable logic such as logging, authentication, caching, or timing in a clean and maintainable way. This article explores the structure, definition, common use cases, and practical examples of decorators in Python.

ادامه

Comprehensive Guide to Python Classes – Object-Oriented Design, Structure, Inheritance, and Practical Use

Classes in Python are the foundation of object-oriented programming, allowing developers to define complex data structures, encapsulate logic, and create scalable, maintainable systems. This article walks through the fundamentals of defining classes, constructors, methods, inheritance, encapsulation, and real-world applications in Python projects.

ادامه

Several example codes

understand the Python programming language

ادامه

Complete Guide to Getting Text Input from Users in Python

In Python, accepting input from users is one of the simplest yet most powerful features for interactive programming. This article explores the input() function in depth, explains how to cast types, validate user data, and use input within loops and conditions. Real-life examples help clarify each concept, making this guide perfect for beginners and intermediate learners.

ادامه

Comprehensive Guide to Functions in Python: Structure, Types, and Use Cases

Functions in Python are essential building blocks for writing modular, reusable, and maintainable code. This article introduces function definitions, distinguishes between built-in and user-defined functions, explores parameters and return values, and presents advanced topics such as recursion and lambda expressions. Whether you're a beginner or brushing up for interviews, this guide offers a clear and accessible foundation.

ادامه

Loops, Iterators, and Repetition Control in Python

Loops, Iterators, and Repetition Control in Python

ادامه