2014-12-02 157 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 
相關問題