0

Please solve this code problem

arr = list(input("enter your 4 digits binary code")) a=int(arr[0]) b=int(arr[1]) c=int(arr[2]) d=int(arr[3]) for i in range(len(arr)): if (int(arr[i])==0 or int(arr[i])==1): if (a,b,c,d==0,0,0,0): print("0") elif(a,b,c,d==0,0,0,1): print("1") elif(a,b,c,d==0,0,1,0): print("2") elif(a,b,c,d==0,0,1,1): print("3") elif(a,b,c,d==0,1,0,0): print("4") elif(a,b,c,d==0,1,0,1): print("5") elif(a,b,c,d==0,1,1,0): print("6") elif(a,b,c,d==0,1,1,1): print("7") elif(a,b,c,d==1,0,0,0): print("8") elif(a,b,c,d==1,0,0,1): print("9") else: print("hi") break else: print("enter binary code") break

26th Apr 2020, 12:17 PM
Shourov Saha
Shourov Saha - avatar
2 odpowiedzi
+ 6
For me the most simple way would be to keep input as string. Then convert it to int, with a second argument for the base system. inp = input('enter binary code: ') if all(i.isdigit() for i in inp): print(int(inp, 2)) else: print('wrong input')
26th Apr 2020, 2:28 PM
Lothar
Lothar - avatar
+ 1
Compare variables via tuples, therefor put them in parentheses: if ((a,b,c,d)==(0,0,0,0)): print("0") Btw. your loop is checking if any of the characters entered is 0 or 1. So if e.g. the you enter 1,5,0,0 it would still print "hi".
26th Apr 2020, 12:32 PM
Manu_1-9-8-5
Manu_1-9-8-5 - avatar