Reputation: 1
How would I loop the following code, when I use if
statements I keep getting an error. I am trying to get the following number printed out, 511887 and Compare each 4-bit binary number using conditional statements but I keep getting an error: NameError: name 'bin_list' is not defined
x=("bin_list")
y=("bin_list[SN_0]")
["0001"]
"0101"
"0001"
"0001"
"1000"
"1000"
"0111"
SN_bin_0 = bin_list[SN_1]
for x in range (0,7):
for index in range(len(L)):
print (y)
SN_bin_1 = bin_list[SN_5]
for x in range (1,7):
for index in range(len(L)):
print (y)
SN_bin_2 = bin_list[SN_1]
for x in range (2,7):
for index in range(len(L)):
print (y)
SN_bin_3 = bin_list[SN_1]
for x in range (3,7):
for index in range(len(L)):
print (y)
SN_bin_4 = bin_list[SN_8]
for x in range (4,7):
for index in range(len(L)):
print (y)
SN_bin_5 = bin_list[SN_8]
for x in range (5,7):
for index in range(len(L)):
print (y)
SN_bin_6 = bin_list[SN_7]
for x in range (6,7):
for index in range(len(L)):
print (y)
Upvotes: 0
Views: 68
Reputation: 625
1. Making a List
Instead of writing:
["0001"]
"0101"
"0001"
"0001"
"1000"
"1000"
"0111"
You can make it a list:
bin_list = ["0101","0001","0001","1000","1000","0111"]
Explanation:
variable = [item1,item2,item3]
bin_List[1]
will return the string "0001"
.2. Value of Variable x
Instead of writing:
x=("bin_list")
You should do:
x=(bin_list)
Explanation:
bin_list
makes it a string. You want the value of x
to be the list bin_list
.x
to it.3. Value of Variable y
Instead of writing:
y=("bin_list[SN_0]")
You should do:
y= bin_list[SN_0]
Explanation:
bin_list[SN_0]
makes it a string. You want the value of y
to be the item at index [SN_0]
of the list bin_list
.4. Undefined variable
The following are undefined:
[SN_0]
[SN_1]
[SN_5]
[SN_8]
L
Each one of them needs a value, for example:
SN_0 = 0
SN_1 = 1
SN_5 = 5
SN_8 = 8
L = x
Explanation:
L
is supposed to refer to the list, you can replace it with x
since the list is already assigned variable x
.4. Indentation
The following code has an extra indentation:
SN_bin_1 = bin_list[SN_5]
for x in range (1,7):
for index in range(len(L)):
print (y)
SN_bin_2 = bin_list[SN_1]
for x in range (2,7):
for index in range(len(L)):
print (y)
SN_bin_3 = bin_list[SN_1]
for x in range (3,7):
for index in range(len(L)):
print (y)
SN_bin_4 = bin_list[SN_8]
for x in range (4,7):
for index in range(len(L)):
print (y)
It should be:
SN_bin_1 = bin_list[SN_5]
for x in range (1,7):
for index in range(len(L)):
print (y)
SN_bin_2 = bin_list[SN_1]
for x in range (2,7):
for index in range(len(L)):
print (y)
SN_bin_3 = bin_list[SN_1]
for x in range (3,7):
for index in range(len(L)):
print (y)
SN_bin_4 = bin_list[SN_8]
for x in range (4,7):
for index in range(len(L)):
print (y)
Upvotes: 1
Reputation: 4469
It looks like there are few issues with your code. The following is not valid python:
x=("bin_list")
y=("bin_list[SN_0]")
["0001"]
"0101"
"0001"
"0001"
"1000"
"1000"
"0111"
I think what you wanted was to have these values in a list, which can be done like so:
bin_list = ["0101", "0001", "0001", "1000", "1000", "0111"]
Next, you are accessing the list using an undefined variable. You need to either use an integer or define SN_0
beforehand.
y = bin_list[0]
or
SN_0 = 0
y = bin_list[SN_0]
Finally, your indentation is off on most of the loops. You have:
SN_bin_1 = bin_list[SN_5]
for x in range (1,7):
for index in range(len(L)):
print (y)
You want:
SN_bin_1 = bin_list[SN_5]
for x in range (1,7): # <-- one less indent here
for index in range(len(L)):
print (y)
I can't exactly tell what your code is meant to do, it seems you are not using many of your variables, and your goal to print the value of y
a certain amount of times. Once you've fixed these errors, open a new question if the code is still not doing what you intended.
Upvotes: 0