0

I have the following code:

for {for loop assertion }:
    if (a) > 0 and b > 0:
        print('same')
    elif (a) < 0 and b < 0:
        print('same')
    else:
        print('different')

This produces a list like this:

different
different
same
different
same
different
same
different
different
same
different
different
different
different
different
same

What I want to do is save this to another variable. Could you please advise how I can accomplish that?

7
  • 2
    Do you know how to add ("append") one value to a list? Commented Jun 8, 2021 at 19:06
  • 2
    While what you want to do literally is possible, is there a reason you're using print? You could just directly append into a list instead. Commented Jun 8, 2021 at 19:06
  • 2
    You haven't created a list at all. You've simply printed stuff the the standard output device. You need to learn to use data structures like list objects and dict objects to organize your code Commented Jun 8, 2021 at 19:06
  • The second if should be an elif, shouldn't it? Commented Jun 8, 2021 at 19:14
  • @VPfB you don't have to use elifs over ifs you could have 7 if statements and that would still be fine Commented Jun 8, 2021 at 19:25

1 Answer 1

3

Try this, it should work, if not tell me why:

lst = []
for {for loop assertion }:
        if (a) > 0 and b > 0:
            lst += ["same"]
        if (a) < 0 and b < 0:
            lst += ["same"]
        else:
            lst += ["different"]

or you could do:

    lst = []
    for {for loop assertion }:
            if (a) > 0 and b > 0:
                lst.append("same")
            if (a) < 0 and b < 0:
                lst.append("same")
            else:
                lst.append("different")
Sign up to request clarification or add additional context in comments.

7 Comments

And also, not needed but if it is a working answer you should click the check mark, so other users when they see it, they will find a working answer.
kk :) @ZaphodBeeblebrox
Can only close it once after 10 mins
btw Zaphod this question isn't exactly closed
that doesnt mean its closed
|

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.