def decToBin(n):
x=str(bin(n))
x=x[2:]
return x
def result(x):
print(len(max(x.split('0'))))
if __name__ == '__main__':
n = int(input().strip())
decToBin(n)
result(x)
I have tried this code but i am getting an error saying undefined "x" I want to know what is wrong in this code.
There are several issues in your code.
return x
result(decToBin(n))
I'm also not sure if this is what you wanted to do according to your question title
Here is the final code:
def decToBin(n):
x = str(bin(n))
x = x[2:]
return x
def result(x):
print(len(max(x.split('0'))))
if __name__ == '__main__':
n = int(input().strip())
result(decToBin(n))
The problem is that the value x
is defined in your method decToBin
, but never returned. So it is only valid inside of that method.
def decToBin(n):
x=str(bin(n))
x=x[2:]
return x
def result(x):
print(len(max(x.split('0'))))
if __name__ == '__main__':
n = int(input().strip())
x = decToBin(n)
result(x)
EDIT: But your code is not counting the ones in a decimal number, but in the binary representation of the number.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.