1

i am trying my first code in python writing classes and objects..

here is the code:

class Order:  
   def __init__(self,A):
       self.a= A

   def user1(self):
    x = len(self.a)
    i =0
    while i < x:
        value = A[i]
        y = value
        return y

A = ["AA","BB","CC","DD","EE","FF"]
honey= Order(A)
print (honey.user1())

i am getting output as AA................but i need to all elements from the A[] ,so only wrote for loop...but it print only output as AA.......

I need output as AA BB CC DD EE FF

how to achieve it using classes and object creation.....help pls

1
  • return y returns a value from the function so the iteration stops. Commented Sep 23, 2017 at 5:32

1 Answer 1

1

what you doing is returning after it loops and gets the first value and thus it gives back only the first value

Also, you are not incrementing your loop this will result in an infinite loop.

something like this is what it should be:

y=[]
while i < x:
    value = A[i]
    y.append(value)
    i+=1
return y
Sign up to request clarification or add additional context in comments.

2 Comments

thank it works fine now...........in python i+=1 is similar to i++ in c?
@dhiyash2008 : Not really. i+=1 in Python is equivalent to i = i + 1. Whereas in C, increment in value by doing i++ reflects in next usage of i and not current (which is not the case with Python here).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.