2016-06-27 34 views
-1
class boy: 
    """Details""" 

    def __init___(self): 
    print("Details") 

    def name(self,x,y,z): 
    a=x.title() + ' ' 
    b=y.title() + ' ' 
    c=z.title() 
    nam = a + b + c 
    print(nam) 

    def age(self,nu): 
    f = int(num) 
    if f>18: 
    return 1 
    else: 
    print("\nSorry you must be at least 18 years to continue") 
    return -1 

    def p_details(self,cc):  #need to print all detals by calling this method. 
    print("Name :" + self.nam) 
    print("Age :" + self.f) 
    print("Country :"+ con) 

a = raw_input("Enter the first name :") 
b = raw_input("Enter the middle name :") 
c = raw_input("Enter the last name :") 
num = raw_input("Enter the age :") 
inp=boy() 

if(inp.age(num) == 1): 
    con = raw_input("\nPlease enter your country :") 
    inp.p_details(con) 

因爲我是pyhton的新手,這個問題可能看起來愚蠢或愚蠢,但我真的很感謝幫助。我需要的是調用p_details並打印所有的細節。同類中的兩種方法如何在Python中訪問它的成員

+0

你忘了傳遞你的參數。 'boy()'應該有a,b,c作爲參數,然後設置'self.age = a'等等。你正在與成員混合變量。檢查python對象語法。 –

+0

你到目前爲止寫的東西在很多層面都有問題(真的很抱歉)。請閱讀一本關於Python的好書,並在你有特定的東西時回來。按照我的觀點,這太寬泛了。 – SuperSaiyan

+0

@SuperSaiyan我會做..作爲首發將有錯誤,很多錯誤。我需要的是一個幫助,因爲我無法理解爲功能和班級工作,..無論如何,我生病了,謝謝 –

回答

0
class boy(): 
    def __init___(self, num): 
     self.age = int(num) 

num = raw_input("Enter the age :") 

inp = boy(num) 

if inp.age < 18: 
    .... 

你的類應該像這樣工作。你寫的方式,inp沒有年齡屬性來評估。但正如在評論中指出的那樣,那裏的代碼真的有很多錯誤。你使用方法的方式表明你不清楚類和方法是如何工作的。另外,獲取用戶輸入會引發一系列問題 - 比如,如果用戶在詢問時沒有輸入數字,而是輸入「足夠老」,會發生什麼?

0

添加類成員到您__init__,像這樣:

def __init___(self,x, y, z): 
    self.x = x 
    self.y = y 
    self.z = z 
    print("Details") 
相關問題