Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
ethanwright
4,267 PointsPython binary converter problem
im having trouble with this code:
con_input = input("1.binary-decimal 2.binary-ASCII 3.binary-hexadecimal\n-\n4.decimal-binary 5.ASCII-binary 6.hexadecimal-binary\n> ")
if con_input != "1" and con_input != "2" and con_input != "3" and con_input != "4" and con_input != "5" and con_input != "6":
print("enter one of the numerical options")
def code_conversion():
if con_input == "1":
print("separate bytes by a space")
binary_input = input("enter binary:\n> ")
int(binary_input)
while len(binary_input) < 8:
print("please enter a 8bits or higher")
binary_input = input("enter binary:\n> ")
while "1" not in binary_input and "0" not in binary_input:
error = ("Please enter binary")
print("\nseparate bytes by a space")
binary_input = input("enter binary:\n> ")
binary_input = list(binary_input)
if [0] in binary_input == '1':
binary_input[0] = '128'
print(binary_input)
code_conversion()
im trying to turn items in the list into their binary to decimal coversion depending if they are a 0 or a 1 however when I try it it does not seem to change to the 128 it just stays as the 1.
any help with this would be greatly appreciated
2 Answers
Chris Freeman
Treehouse Moderator 68,468 PointsThanks for reformatting your code. There is still an extra space preceeding lines in the bottom part of your code. But let's move on....
Looking at the function code_conversion(), I've added comments to your code trying to figure out your intentions:
def code_conversion():
if con_input == "1":
# convert binary to decimal
print("separate bytes by a space")
binary_input = input("enter binary:\n> ")
# use int() to check for non-numbers
int(binary_input)
# loop until input has length 8 or more
while len(binary_input) < 8:
print("please enter a 8bits or higher")
binary_input = input("enter binary:\n> ")
# loop until input contains either a "1" or a "0"
while "1" not in binary_input and "0" not in binary_input:
error = ("Please enter binary")
print("\nseparate bytes by a space")
binary_input = input("enter binary:\n> ")
# convert binary_input to list
binary_input = list(binary_input)
# if "list with 0 as element" is in boolean value from expression "binary_input == '1'"
# this can't be what you want
if [0] in binary_input == '1':
# replace first character with string '128'
binary_input[0] = '128'
# print binary_input
print(binary_input)
for the last if block, perhaps you meant:
# if first element is '1', replace with '128'
if binary_input[0] == '1':
# replace first character with string '128'
binary_input[0] = '128'
I would rethink two parts of your code.
First, during checking of the user input binary_input, the separate sections are not cummulative. That is, at each point the user could enter different code that passes one check but fails previous checks. Look at the following input I ran in ipython:
In [39]: code_conversion()
separate bytes by a space
enter binary:
> 0
please enter a 8bits or higher
enter binary:
> aaaaaaaaa
separate bytes by a space
enter binary:
> 1aaa
['128', 'a', 'a', 'a']
I changed my input and got through with non-binary data. Input checkout should be in one loop:
# import regexp module
import re
while True:
basic_input = input("> ")
# check input length > or = 8, and
# using re, check input contains only "1", "0", space
if len(basic_input) >= 8 and re.search(r'^[10 ]*$', basic_input):
print(basic_input)
break
The other part to consider is the length of binary_input. If over 8 characters, then the first item binary_input[0] might have a value more than 128
ethanwright
4,267 Pointsthanks!
ethanwright
4,267 Pointsthat should work :)
Chris Freeman
Treehouse Moderator 68,468 PointsChris Freeman
Treehouse Moderator 68,468 PointsPlease edit to add the word python and newline after first triple-backtick