1

I need a help to update my theta value for each last two numbers for 5 element in the message:

msg = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15]
def get_new_theta(msg):
    # addition for each last 2 number in 5 elements in the msg
    # for example: new_theta=theta+(4+5), new_theta=new_theta+(9+10) and so on...
    return new_theta

#initial theta
theta=1 
for b in msg:
    hwpx = [0,math.cos(4*math.radians(theta)),math.sin(4*math.radians(theta)), 0]
    a=b*hwpx
    print (a)

This is the expected output:

theta=1
1*[0,math.cos(4*math.radians(1)),math.sin(4*math.radians(1)), 0]
2*same as above
3*same as above
4*same as above
5*same as above
theta=1+(4+5)=10
6*[0,math.cos(4*math.radians(10)),math.sin(4*math.radians(10)), 0]
7*same as above
8*same as above
9*same as above
10*same as above
theta=10+(9+10)=29

Noted that the value of theta will be update for each 5 element. And the new theta will be used to calculated for the next element.

However,when I run this code, loop was not successfully implemented.

msg = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15]
theta=1
def get_new_theta(msg, theta):
    new_theta = [theta]
    for a, b in zip(msg[3::5], msg[4::5]):
        new_theta.append(new_theta[-1] + a + b)
    return new_theta

theta=1

for b in msg:
    theta=get_new_theta(msg, theta)
    hwpx = [0, math.cos(4*math.radians(theta)), math.sin(4*math.radians(theta)), 0]
    a=b*hwpx
    print (theta)

I got this error:

hwpx = [0, math.cos(4*math.radians(theta)), math.sin(4*math.radians(theta)), 0]
TypeError: a float is required`

Thank you

9
  • Are you wanting a new theta value every time you iterate through the msg list? Also, b * [list] will give you a list with the original values copied out b times. Is this what you want? Commented Aug 9, 2018 at 3:46
  • @PeptideWitch The expected output is updated in the question. Commented Aug 9, 2018 at 4:00
  • Your function returns a list, so you need to access the desired theta from the list using an index Commented Aug 9, 2018 at 4:50
  • @ PeptideWitch Still got the same error even I changed it to float. Commented Aug 9, 2018 at 4:54
  • I've edited my post above, apologies. Commented Aug 9, 2018 at 4:57

3 Answers 3

2

try

def get_new_theta(msg, theta):
    new_theta = [theta]
    for a, b in zip(msg[3::5], msg[4::5]):
        new_theta.append(new_theta[-1] + a + b)
    return new_theta

or if you prefer

def get_new_theta(msg, theta):
    yield theta
    for a, b in zip(msg[3::5], msg[4::5]):
        theta += a + b
        yield theta

Edit

It feels a bit weird that my answer got accepted but your problem is not truly solved yet...

Now the problem is that you've asked about theta, but didn't tell us what you truly want. What is hwpx? Is it supposed to be a list? For now, I can only provide something like

# get_new_theta() returns [1, 10, 29, 58] 
# but I don't know what to do with the last 58 so slice it out
for i, theta in enumerate(get_new_theta(msg, 1)[:-1]):
    for j in range(5):
        print(theta, msg[i*5+j])
        # you may want to do msg[i*5+j] * f(theta) here.

1 1
1 2
1 3
1 4
1 5
10 6
10 7
10 8
10 9
10 10
29 11
29 12
29 13
29 14
29 15
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much. Finally, I got the best answer.
0

Does this do what you want?

def get_new_theta(msg, theta):
    new_theta = theta + msg[-2] +msg[-1]
    return new_theta

1 Comment

Max Caudle The value of theta need to be updated for each 5 elements. Hopefully you can show the loop for the codes.
0

Try:

msg = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15]
def get_new_theta(msg, theta):
    return theta + sum(msg[3::5]) + sum(msg[4::5])
theta=1
print(get_new_theta(msg, theta))

This outputs: 58 (1 + 4 + 5 + 9 + 10 + 14 + 15 = 58)

2 Comments

I believe OP wants: 'new_theta=theta+(4+5), new_theta=new_theta+(9+10)', so a list position that changes, whereas your example provides a static 3-5, 4-5 within the msg list. (ps didn't downvote you)
@ PeptideWitch yes. exactly. I already updated the expected output in the question.

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.