小白教程

 找回密码
 立即注册
查看: 6249|回复: 0

小白教程:python3,利用函数求两个数的和与差

[复制链接]

176

主题

185

帖子

663

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
663
发表于 2021-4-10 06:52:00 | 显示全部楼层 |阅读模式

基础内容:

相信很多刚接触 python 的同学都有遇到过这种题。以下是 在 python 中的利用函数求两个数的和与差。

  1. class Calculate:   
  2.     def ——init——(self,number1,number2)        # 接收两个参数
  3.         self.number1 = number1
  4.         self.number2 = number2
  5.    
  6.     def he(self):
  7.         print("和是 : %d " % (self.number1 + self.number2))    # 进行格式化输出
  8.      
  9.     def cha(self):
  10.         print("差是 : %d " % (self.number1 - self.number2))




  11. result = Calculate(5,5)        # 传递两个参数
  12. result.he()
  13. result.cha()            # 调用函数进行求值
复制代码


回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|小黑屋|小白教程 ( 粤ICP备20019910号 )

GMT+8, 2024-9-20 14:30 , Processed in 0.031065 second(s), 23 queries .

Powered by Discuz! X3.4

© 2001-2017 Comsenz Inc. Template By 【未来科技】【 www.wekei.cn 】

快速回复 返回顶部 返回列表