+ 2

Why will my program not store bit? Solved

def Bit(x): bit = [] if x == 1: bit.append(1) return bit elif x == 0: bit.append(0) return bit else: while x > 0: n = x % 2 bit.insert(0,n) x = x / 2 return bit Bit(1) print(bit)

1st Feb 2018, 12:20 AM
Camryn Cleveland
Camryn Cleveland - avatar
5 odpowiedzi
+ 3
You defined the bit variable inside of the Bit function. That's why you can't access it from outside. But since you return it from the function, you can assign it to a new variable. If you do: bit = Bit(1) print(bit) You're assigning the result of the Bit function call to a new variable called bit, and printing that.
1st Feb 2018, 12:32 AM
SplittyDev
SplittyDev - avatar
+ 2
thanks that was really helpful. : )
1st Feb 2018, 12:34 AM
Camryn Cleveland
Camryn Cleveland - avatar
+ 1
You did not assign the result of the Bit function call to a variable. Try bit = Bit(1)
1st Feb 2018, 12:22 AM
SplittyDev
SplittyDev - avatar
+ 1
oh thank you but how am I suppose to store the bit if it is not a integer
1st Feb 2018, 12:24 AM
Camryn Cleveland
Camryn Cleveland - avatar
+ 1
wait why bit = Bit 1
1st Feb 2018, 12:27 AM
Camryn Cleveland
Camryn Cleveland - avatar