如何将我的fun.py转换为模块,以便可以在untitled.py中使用它?
class test:
def __init__(self,num0,num1):
self.num0 = num0
self.num1 = num1
def add(self):
self.num0 + self.num1
def sub(self):
self.num0 - self.num1
def mul(self):
self.num0 * self.num1
def div(self):
self.num0 / self.num1
from a import fun
a = eval(input('enter a:'))
b = eval(input('enter b:'))
test = fun.test(a,b)
print(test.add())
答案 0 :(得分:0)
您当前的代码很少出现问题,
# fun.py
class test:
def __init__(self,num0,num1):
self.num0 = num0
self.num1 = num1
'''
You were just doing operations on variables,
you need to return the values as well
'''
def add(self):
return self.num0 + self.num1
def sub(self):
return self.num0 - self.num1
def mul(self):
return self.num0 * self.num1
def div(self):
return self.num0 / self.num1
# from {filename} import {class}
from fun import test
# You need to convert inputs into int() as input() returns string
a = int(input('enter a:'))
b = int(input('enter b:'))
test = test(a,b)
print(test.add())