On 2012-2-07 01:18, Chappman wrote:
Hi Rob,
with this syntex:
x=0
for y_1 in [1..2]:
for y_2 in [1..y_1]:
x += [y_1,y_2]
print x
what I am trying to do is, trying to use the two numbers y_1 and y_2
in x +=[y_1,y_2]
to assign it a number from previously set conditions
if y_1 = y_2:
y_1 = y_2 = 2
elif y_1>y_2:
y_1 = y_2 = 1
but currently my code is having trouble doing that.
Is there a way to do this please?
Are you trying to define [u,v] as a function whose value is 2 if the
arguments are equal and 1 if u>v? (What if v<u?) Among other syntactic
problems, you can't do that with [], because that symbol is reserved for
lists.
Here's how I'd do what I think you're trying to do:
# define a function of two inputs
def chap(u,v):
if u==v: return 2
# no 'else' needed, because 'return' breaks out of the function
if u>v: return 1
return None # ought to be a numeric value
x=0
for y1 in range(1,3):
for y2 in range(1,y1+1):
x += chap(y1,y2)
print x
--
Anton Sherwood *\\* www.bendwavy.org *\\* www.zazzle.com/tamfang
--
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/sage-support
URL: http://www.sagemath.org