-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
87 lines (64 loc) · 2.13 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# Declares the decimal to binary conversion (decToBin)
def decToBin():
print("Typ hier uw decimale nummer in (integraal)")
decimalInput = int(input())
binary = []
# function converts decimal to binary by dividing by 2 and checking for remainders.
while decimalInput != 0:
if decimalInput % 2 == 0:
decimalInput = decimalInput / 2
binary.insert(0,0)
else:
decimalInput = (decimalInput - 1) / 2
binary.insert(0,1)
print(binary)
# Declares the binary to decimal conversion (binToDec)
def binToDec():
arrayCounter = 0
decResult = 0
counter = 0
print("Typ hier uw binaire nummer in")
strInput = input()
# Checks if input is binary
checkBin = set(strInput)
checkSource = {'0','1'}
if checkSource == checkBin or checkBin == {'0'} or checkBin == {'1'}:
print(" ")
else:
print("dit is geen binair getal!")
print(" ")
return
# Separates the string into separate digits in an array and defines the length of it
convertedArray = [int(d) for d in str(strInput)]
arrayLength = len(convertedArray)
# Sets power raised
power = arrayLength - 1
# Converts binary to decimal by multiplying the numbers by 2 to the correct power (defined by the array length)
while arrayLength != 0:
decResult = decResult + convertedArray[counter] * 2 ** power
counter = counter + 1
power = power - 1
arrayLength = arrayLength - 1
print(decResult)
# start of main function
while True:
print("Binair naar decimaal of decimaal naar binair?")
print ("A=Dec-bin B=Bin-dec C=Programma verlaten")
# User input to choose converting method or to quit
opt = input()
if opt == "A" or opt == "a":
print(" ")
decToBin()
print(" ")
elif opt == "B" or opt == "b":
print(" ")
binToDec()
print(" ")
elif opt == "C" or opt == "c":
print(" ")
print("Dag!")
break
else:
print(" ")
print("Selecteer \"a\", \"b\" of \"c\" a.u.b")
print(" ")