当前位置: 首页 > news >正文

《Python语言程序设计》第4章第7题,这次利用之前学过的第7章的内容使用对象和类,来修改这道题

在这里插入图片描述


original codeamount = eval(input('Enter an amount, for example,11.56: '))#偷懒直接做了输入
amount = 11.56remain_in_amount = int(amount * 100)number_of_one_dollars = remain_in_amount // 100
remain_in_amount = remain_in_amount % 100number_of_quarters = remain_in_amount // 25
remain_in_amount = remain_in_amount % 25number_of_dimes = remain_in_amount // 10
remain_in_amount = remain_in_amount % 10number_of_nickels = remain_in_amount // 5
remain_in_amount = remain_in_amount % 5number_of_pennies = remain_in_amountif amount >1:amount_text= 'consists'
elif amount==1:amount_text= 'consist'print('Your amount',amount,'consists of\n','\t', number_of_one_dollars,'dollars\n','\t',number_of_quarters,'quarters\n','\t',number_of_dimes,'dimes\n','\t',number_of_nickels,'nickels\n','\t',number_of_pennies,'pennies')

原始的输出结果

在这里插入图片描述

我们使用第七章对象和类的功能

请用户输入钱数
将输入的整数或者浮点数进行整数化

class amount_com:def __init__(self, amount):self.amount = amountdef get_int_amount(self):text_nickels = ''text_dollars = ''text_quarters = ''text_dimes = ''text_pennies = ''remain_in_amount = int(self.amount * 100)number_of_one_dollars = remain_in_amount // 100remain_in_amount = remain_in_amount % 100number_of_quarters = remain_in_amount // 25remain_in_amount = remain_in_amount % 25number_of_dimes = remain_in_amount // 10remain_in_amount = remain_in_amount % 10number_of_nickels = remain_in_amount // 5remain_in_amount = remain_in_amount % 5number_of_pennies = remain_in_amountif number_of_one_dollars == 1:text_dollars = 'dollar'elif number_of_one_dollars > 1:text_dollars = 'dollars'if number_of_quarters == 1:text_quarters = 'quarter'elif number_of_quarters > 1:text_quarters = 'quarters'if number_of_nickels == 1:text_nickels = 'nickel'elif number_of_nickels > 1:text_nickels = 'nickels'if number_of_dimes == 1:text_dimes = 'dime'elif number_of_dimes > 1:text_dimes = 'dimes'if number_of_pennies == 1:text_pennies = 'pennie'elif number_of_pennies > 1:text_pennies = 'pennies'print(f"""Your amount {self.amount} consists of:{number_of_one_dollars}, {text_dollars}{number_of_quarters}, {text_quarters}{number_of_dimes}, {text_dimes}{number_of_nickels}, {text_nickels}{number_of_pennies}, {text_pennies}""")num = 11.56
a = amount_com(num)
a.get_int_amount()

在这里插入图片描述
运行效果。

相关文章:

  • 跟Gemini学做PPT:字号选择
  • 智能交易革命:基于Python构建全自动加密货币交易系统(CCXT/TensorTrade实战指南)
  • CyberStrikeLab-Lab9-WriteUp
  • AI 数据采集实战指南:基于 Bright Data 快速获取招标讯息
  • C++高频面试考点 -- 智能指针
  • Android Studio历史版本下载方法
  • 2025年电工杯A题第一版本Q1-Q4详细思路求解+代码运行
  • 【Python 算法零基础 4.排序 ⑤ 归并排序】
  • 【软考架构】2025系统架构设计师开坑指南——后端开发(科目选择,考试大纲,真题分析)
  • ROS合集(七)SVIn2声呐模块分析
  • 后端环境配置
  • 【黑马JavaWeb+AI知识梳理】后端Web基础03 - MySQL概述
  • 服务器日志监控与分析:提前发现黑客攻击迹象
  • 基于SpringBoot+Vue的社区医院信息平台设计与实现
  • Redis淘汰策略
  • 如何使用CAMEL框架来搭建一个简单实用的**旅游出行规划助手**
  • AI架构分层原则
  • Flask-SQLAlchemy数据库查询:query
  • DataX 的大概简单介绍(与Kettle做对比介绍)
  • 华为云Astro前端页面数据模型选型及绑定IoTDA物联网数据实施指南
  • 做网站时的尺寸/建立网站的流程
  • 怎么自己在家做网站/成都百度推广开户公司
  • wordpress建站镜像/提交网址给百度
  • 做一家算命的网站/培训机构优化
  • 网站建设 cn/怎么注册网址
  • 怎么建一个小说网站/百度手机助手下载安装最新版