Advertisement
Guest User

Untitled

a guest
May 25th, 2019
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.49 KB | None | 0 0
  1. def take_input(): #loop to take input for the 32 digit binary number
  2.     userInput=None;
  3.     binaryFlag=None; #i mightve made too many bool flags but this was annoying, binaryFlag changes to false if the number isnt binary to stop the loop
  4.     loopFlag=True; #loopflag only exists for the while loop statement, itll almost always be true
  5.     while loopFlag==True:
  6.         binaryFlag=True;   #set this to true initially within the loop so every loop it starts true
  7.         userInput=input("Enter a 32 digit binary number: ");
  8.         if len(userInput)==32:   #first checks if theres 32 numbers in the string
  9.             print("length ok");  #if there is, prints a simple debug statement "length ok"
  10.             for x in userInput:     #same for statement as an earlier project, individually goes through the string to make sure its all 1s and 0s
  11.                 if x=="1" or x=="0":
  12.                     binaryFlag=True;
  13.                 else:
  14.                     print(x,"not binary!",sep=" "); #if it isnt all 1s and 0s, break the loop and set binaryFlag to False, to show it isnt binary
  15.                     binaryFlag=False;
  16.                     break;
  17.             if binaryFlag==True: #if the number given is binary, and 32 digits (we already know because its nested within the length if statement)
  18.                 loopFlag=False; #useless statement, just insurance
  19.                 return userInput; #then end this function, returning the userinput
  20.         else:
  21.             print("make sure the number is 32 digits");
  22. def split_string(binaryString): #this function splits the string into 4 8 digit strings
  23.     length=8;
  24.     return [binaryString[i:i+length] for i in range(0, len(binaryString),length)]; #i dont know why this syntax is like this, but this is what the assignment said to do
  25.  
  26. def convert_binary(binary): #function to convert binary to decimal
  27.     dec=0;
  28.     for x in str(binary):   #if you notice, this is the same exact code we used for the other program we had to convert binary to decimal, just in a function this time
  29.         if x=="1" or x=="0":
  30.             dec=dec*2+int(x)
  31.         else:
  32.             print("make sure your number is binary!")
  33.             break;
  34.     return dec;
  35.  
  36. def print_results(binaryList):       #function to print the results
  37.     print(convert_binary(binaryList[0]),end=".");
  38.     print(convert_binary(binaryList[1]),end="."); #these have "end="."" because the format the program has to print out is just separated with periods, not entire new lines
  39.     print(convert_binary(binaryList[2]),end=".");
  40.     print(convert_binary(binaryList[3]));
  41.  
  42. #this is the actual program, all it does is call the functions we already wrote
  43. binaryString=take_input();
  44. binaryList=split_string(binaryString)
  45. print_results(binaryList);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement