How to use for loops and assign values to array

asked 2021-01-18 11:34:33 +0200

anonymous user


updated 2021-01-18 15:55:50 +0200

x = 3 z = IncreasingArrays() z = x.binary()

for i in range(x): if z[i] == 1: print(' ', 2^i) # Not printing here

I'm unable to print the 3rd line it always says space indent etc.

How to solve this? Also please help me how to assign values in an array in for loop

Also binary values I should store in array how to??

I'm new please help

I'm using this only on sagemath cell server online


edit retag flag offensive close merge delete


Could you please provide your code ? To format it, you select the code and click on the 101011 button on top of the edition box.

tmonteil gravatar imagetmonteil ( 2021-01-18 11:53:23 +0200 )edit

Thanks for the response

x=3 z=IncreasingArrays() z=x.binary()

for i in range(x): if(z[i]==1): print (' ',2^i) #Not printing here

I want to print if binary value is 1 And raise it to power 2

For loop isn't printing print statement also Please help me with these for loops and also how to store values in an array

in C I go

int a[6]; for(i=2;i<=5;i++) { a[i]=1; }

Here like this in C, array values are set.

How to do the same in sage please help

Rama gravatar imageRama ( 2021-01-18 13:45:54 +0200 )edit

Still no indentation. We can't help you without it : In Python, whitespace (indentation, line feeds) is syntactic.

Emmanuel Charpentier gravatar imageEmmanuel Charpentier ( 2021-01-18 15:20:12 +0200 )edit

Now please check again I have written the program.

Also how to initiate elements in array, please tell me

Rama gravatar imageRama ( 2021-01-18 15:56:32 +0200 )edit