IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> Python知识库 -> Python语言程序设计Y.Daniel Liang练习题Chapter7.2 -> 正文阅读

[Python知识库]Python语言程序设计Y.Daniel Liang练习题Chapter7.2

list7.7_TeseLoanClass.py

from Loan import Loan

def main():
    # Enter yearly interest rate
    annualInterestRate = eval(input("Enter yearly interest rate, for example, 7.25: "))

    # Enter number of years
    numberOfYears = eval(input("Enter number of years as an integer: "))

    # Enter loan amount
    loanAmount = eval(input("Enter loan amount, for example, 120000.95: "))

    # Enter a borrower
    borrower = input("Enter a borrower's name: ")

    # Create a Loan object
    loan = Loan(annualInterestRate, numberOfYears,loanAmount, borrower)

    # Display loan date, monthly payment, and total payment
    print("The loan is for", loan.getBorrower())
    print("The monthly payment is",format(loan.getMonthlyPayment() , ".2f"))

    print("The total payment is",format(loan.getTotalPayment() , ".2f"))

main() # Call the main function

?

?

list7.8_Loan.py

class Loan:
    def __init__(self, annualInterestRate = 2.5,
                 numberOfYears = 1, loanAmount = 1000, borrower = " "):
        self.__annualInterestRate = annualInterestRate
        self.__numberOfYears = numberOfYears
        self.__loanAmount = loanAmount
        self.__borrower = borrower

    def getAnnualInterestRate(self):
        return self.__annualInterestRate

    def getNumberOfYears(self):
        return self.__numberOfYears

    def getLoanAmount(self):
        return self.__loanAmount

    def getBorrower(self):
        return self.__borrower

    def setAnnualInterestRate(self, annualInterestRate):
        self.__annualInterestRate = annualInterestRate

    def setNumberOfYears(self, numberOfYears):
        self.__numberOfYears = numberOfYears

    def setLoanAmount(self, loanAmount):
        self.__loanAmount = loanAmount

    def setBorrower(self, borrower):
        self.__borrower = borrower

    def getMonthlyPayment(self):
        monthlyInterestRate = self.__annualInterestRate / 1200
        monthlyPayment = \
            self.__loanAmount * monthlyInterestRate / (1 - (1 /
            (1 + monthlyInterestRate) ** (self.__numberOfYears * 12)))
        return monthlyPayment

    def getTotalPayment(self):
        totalPayment = self.getMonthlyPayment() * \
                       self.__numberOfYears * 12
        return totalPayment

list7.9_UseBMIClass.py

from BMI import BMI

def main():
    bmi1 = BMI("John Doe", 18, 145, 70)
    print("The BMI for", bmi1.getName(), "is", bmi1.getBMI(), bmi1.getStatus())

    bmi2 = BMI("Peter King", 50, 215, 70)
    print("The BMI for", bmi2.getName(), "is", bmi2.getBMI(), bmi2.getStatus())

main()

?

?

list7.10_BMI.py

class BMI:
    def __init__(self, name, age, weight, height):
        self.__name = name
        self.__age = age
        self.__weight = weight
        self.__height = height

    def getBMI(self):
        KILOGRAMS_PER_POUND = 0.45359237
        METERS_PER_INCH = 0.0254
        bmi = self.__weight * KILOGRAMS_PER_POUND / \
              ((self.__height * METERS_PER_INCH) * \
               (self.__height * METERS_PER_INCH))
        return round(bmi * 100) / 100

    def getStatus(self):
        bmi = self.getBMI()
        if bmi < 18.5:
            return "Underweight"
        elif bmi < 25:
            return "Normal"
        elif bmi < 30:
            return "Overweight"
        else:
            return "Obese"

    def getName(self):
        return self.__name

    def getAge(self):
        return self.__age

    def getWeight(self):
        return self.__weight

    def getHeight(self):
        return self.__height

exe7.3

class Account:
    def __init__(self, id=0, balance=100.0, annualIntersetRate=0.0):
        self.__id = id
        self.__balance = balance
        self.__annualIntersetRate = annualIntersetRate

    def getId(self):
        return self.__id
    def getBalance(self):
        return self.__balance



    def getMonthlyInterestRate(self):
        return self.__annualIntersetRate / 12

    def getMonthlyInterest(self):
        return self.__annualIntersetRate * self.__balance / 12

    def withdraw(self, balance):
        self.__balance = self.__balance-balance
        return self.__balance

    def deposit(self, balance):
        self.__balance = self.__balance+balance
        return self.__balance



def main():
    Account1 = Account(id=1122, balance=20000, annualIntersetRate=0.045)
    Account1.withdraw(2500)
    Account1.deposit(3000)
    print("id = ",Account1.getId())
    print("balance =", Account1.getBalance())
    print("monthly interest rate =", Account1.getMonthlyInterestRate())
    print("monthly interest =",Account1.getMonthlyInterest())

main()

?

?

exe7.4

SLOW = 1
MEDIUM = 2
FAST = 3
class Fan:
    def __init__(self, on=False, radius=5, speed=SLOW, color="blue"):
        self.__speed = speed
        self.__on = on
        self.__radius = radius
        self.__color = color

    def getSpeed(self):
        return self.__speed
    def getOn(self):
        return self.__on
    def getRadius(self):
        return self.__radius
    def getColor(self):
        return self.__color


def main():
    Fan1 = Fan(on=True, radius=10, speed=FAST, color="yellow")
    Fan2 = Fan(on=False, radius=5, speed=MEDIUM, color="blue")
    print("Speed=", Fan1.getSpeed(), "On=", Fan1.getOn(),
          "getRadius=", Fan1.getRadius(), "Color=", Fan1.getColor())
    print("Speed=", Fan2.getSpeed(), "On=", Fan2.getOn(),
          "getRadius=", Fan2.getRadius(), "Color=", Fan2.getColor())

main()

?

?

exe7.5

import math
class RegularPolygon:
    def __init__(self, n=3, side=1, x=0, y=0):
        self.__n = n
        self.__side = side
        self.__x = x
        self.__y = y

    def getPerimeter(self):
        perimeter = self.__n * self.__side
        return perimeter

    def getArea(self):
        area = (self.__n * self.__side * self.__side) / (4 * math.tan(math.pi / self.__n))
        return area


def main():
    RegularPolygon1 = RegularPolygon()
    RegularPolygon2 = RegularPolygon(6, 4)
    RegularPolygon3 = RegularPolygon(10, 4, 5.6, 7.8)
    print("Area1=", format(RegularPolygon1.getArea(), ".4"), "Perimeter1=", RegularPolygon1.getPerimeter())
    print("Area2=", format(RegularPolygon2.getArea(), ".4"), "Perimeter2=", RegularPolygon2.getPerimeter())
    print("Area3=", format(RegularPolygon3.getArea(), ".4"), "Perimeter3=", RegularPolygon3.getPerimeter())

main()

?

  Python知识库 最新文章
Python中String模块
【Python】 14-CVS文件操作
python的panda库读写文件
使用Nordic的nrf52840实现蓝牙DFU过程
【Python学习记录】numpy数组用法整理
Python学习笔记
python字符串和列表
python如何从txt文件中解析出有效的数据
Python编程从入门到实践自学/3.1-3.2
python变量
上一篇文章      下一篇文章      查看所有文章
加:2021-11-11 12:39:47  更:2021-11-11 12:41:53 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年11日历 -2024/11/15 23:36:44-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码