kiran_raj_r kiran_raj_r - 5 months ago 8
Python Question

how can two methods in same class can access it members in python

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)


As i am new into pyhton the question may seem silly or foolish but i really appreciate a help. What i need is to call p_details and print all the details.

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

num = raw_input("Enter the age :")

inp = boy(num)

if inp.age < 18:
    ....

Your class should work like this. The way you wrote it, inp has no age attribute to evaluate. But as pointed out in the comments, there's really a lot wrong with your code there. The way you're using methods suggests you aren't clear on how classes and methods work, exactly. Also, getting user input opens up a whole host of issues - like what happens if the user doesn't enter a numeral when asked, and instead types, say, "old enough"?