+ 2
Could anyone make my code a little cleaner?
I am just starting coding and had an idea for a simple project. I made a random binary generator that also prints the decimal version. If you could let me know if I could make it cleaner I would live the input. I also plan on making a binary interpreter for a user input. https://code.sololearn.com/cQuXrEo2cRzA/?ref=app Thanks
2 Answers
+ 4
Nice work. You could compactify the conversions a bit:
strNum = "".join([str(i) for i in array])
deciNum = int(strNum, base=2)
+ 3
Looks pretty clean!
Do you need the string for something? If not, you can erase all the string related operations and just (looks the same) write:
print(*array, sep='')
One more thing: You can lose the variable 'count' (including '+= 1') completely. Write:
for count, i in enumerate(array):
Then each element gets a number that increments automatically.
One more thing. You might consider defining your array directly:
array = [r.randint(0, 1) for i in range(8)]
That loses you the whole loop. All taken together your code will be ten lines shorter.
(If it looks cleaner might be a matter of taste.)