2014-12-02 197 views
-1

我写了一个函数wczytaj以获取所有参数,我想将它们返回给构造函数,但它不以这种方式工作。我不知道为什么不和如何解决它替代构造函数缺少必需的位置参数

我得到这个错误:

TypeError: wczytaj() missing 3 required positional arguments: 'a', 'b', and 'c' 

是不可能写一个函数,返回3个参数?

from math import sqrt 

def wczytaj(a , b , c): 
     a = input("Podaj parametr A? ") 
     b = input("Podaj parametr B ") 
     c = input("Podaj parametr C? ") 
     return a , b , c 

class Rk: 

     def __init__(self,a,b,c): 
     self.a = a 
     self.b = b   
     self.c = c 


nowe = Rk(wczytaj()) 


print("Ten program rozwiązuje równanie kwadratowe po podaniu parametrów.") 
print("\n Równianie jest postaci {}x*x + {}x + {} = 0 ".format(a, b, c), end="") 
+2

为什么你不这样做[你已经显示](http://stackoverflow.com/a/27250497/3001761)?我已经删除了请求*“解释我[OOP]”*;对于SO来说这个问题太广泛了。 – jonrsharpe 2014-12-02 14:18:10

回答

2

wczytaj功能

def wczytaj(): 
     a = input("Podaj parametr A? ") 
     b = input("Podaj parametr B ") 
     c = input("Podaj parametr C? ") 
     return a , b , c 

然后,你必须使用*运营商返回的值作为参数解压缩到类__init__

nowe = Rk(*wczytaj()) 

你可以看到删除参数现在当我输入分别为1,2,3 ,成员现在设置

>>> nowe.a 
1 
>>> nowe.b 
2 
>>> nowe.c 
3 
相关问题