一个实操
Topic source#!/usr/bin/env python3
# -*- coding: utf-8 -*-
class Com(object):
def __init__(self):
self.n1=float(input('输入一个数字:'))
self.n2=float(input('再输入一个数字:'))
def compa(self):
if self.n1>self.n2:
return str(self.n1)+'大于'+str(self.n2)
elif self.n1==self.n2:
return str(self.n1)+'等于'+str(self.n2)
else:
return str(self.n1)+'小于'+str(self.n2)
x=Com()
print('第一个数字:''%s' % (x.n1)+'\n''第二个数字:''%s' % (x.n2))
print(x.compa())
# ! /usr/bin/env python
# -*- coding:utf-8 -*-
class Com:
def __init__(self,num1,num2):
self.num1=num1
self.num2=num2
def compa(self):
if self.num1 > self.num2:
return str(self.num1)+'>'+str(self.num2)
elif self.num1 == self.num2:
return str(self.num1)+'='+str(self.num2)
else:
return str(self.num1)+'<'+str(self.num2)
M0=input('input a num:')
M1=input('input a num again:')
print('first num:%s' % M0+'\n'+'two num:%s' % M1)
x=Com(M0,M1)
print(x.compa())
Com类定义部分不变,外部方法:
def collect():
M = []
M0 = input('first one: ')
M1 = input('second one: ')
M.append(M0)
M.append(M1)
return M
def compare():
M = collect()
x = Com(M[0],M[1])
x.compa()
if __name__ == '__main__':
compare()
按照你之前的写法,collect()就是用来获得用户输入的,如果想写到Com类定义中去,可以参考1楼~
- 1
慢热程某某
为什么collect函数 不能放在Com类下面呢?