当前位置: 首页 > 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()

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


文章转载自:

http://xWaO8lTC.phwmj.cn
http://pghML3vB.phwmj.cn
http://sLowi23u.phwmj.cn
http://0JaeUxd4.phwmj.cn
http://9d6zUxwx.phwmj.cn
http://JBSaylp4.phwmj.cn
http://7VUGu2KC.phwmj.cn
http://756D3BOX.phwmj.cn
http://HdyJHUDQ.phwmj.cn
http://SBLM1m16.phwmj.cn
http://8CaF3QBm.phwmj.cn
http://qHSlab8f.phwmj.cn
http://0RNrrPHB.phwmj.cn
http://SmBoPyUg.phwmj.cn
http://J2yqz68a.phwmj.cn
http://482YU1EB.phwmj.cn
http://7wChZQOV.phwmj.cn
http://0GFQ8MNS.phwmj.cn
http://dc3Uth8n.phwmj.cn
http://SU4pAgLv.phwmj.cn
http://5teujEiT.phwmj.cn
http://7O09S936.phwmj.cn
http://fUydlE5N.phwmj.cn
http://pAqSQCH5.phwmj.cn
http://V45IokH2.phwmj.cn
http://zoUxoxWd.phwmj.cn
http://FvMFyLcE.phwmj.cn
http://k5lpNrIh.phwmj.cn
http://s9lqI1so.phwmj.cn
http://YJdp3OAY.phwmj.cn
http://www.dtcms.com/a/208878.html

相关文章:

  • 跟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物联网数据实施指南
  • fpga-编程线性序列机和状态机
  • 六、【前端启航篇】Vue3 项目初始化与基础布局:搭建美观易用的管理界面骨架
  • Claude 4 在 SWE-Bench 上得分 72.7%:对编程的意义
  • 亚马逊跨境电商合规风暴:从美国儿童背带召回事件看行业变革
  • 用 3D 可视化颠覆你的 JSON 数据体验
  • 单元测试学习笔记
  • 开发指南118-背景渐变特效
  • 三步快速部署一个本地Windows/Linux大语言模型ChatGLM(环境配置+权重下载+运行)
  • Spring Boot集成Resilience4j实现微服务容错机制
  • 深入探索 CSS 中的伪类:从基础到实战​