-
Notifications
You must be signed in to change notification settings - Fork 0
/
Resistor colour code calculator.py
102 lines (89 loc) · 3.1 KB
/
Resistor colour code calculator.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#Resistor colour code calculator
#intro
print("Welcome to the resistor colour code calculator!")
print("This simple program will help you work out the value of your resistor without using any of those silly acronyms!")
#defining colour values:
colours = {
"black": 0,
"brown": 1,
"red": 2,
"orange": 3,
"yellow": 4,
"green": 5,
"blue": 6,
"violet": 7,
"grey": 8,
"white": 9,
}
#creating functions to find the values of different band resistors:
def four_bands(col): #if a resistor is 4 band, the last band is tolerance
inks = []
counter = 0
while counter < 3:
counter += 1
print(f"please enter band {counter}")
temp = input("enter band colour:")
temp.lower
colours_matching = 0
for key, value in colours.items():
if temp == key:
colours_matching += 1
if colours_matching != 1:
colours_matching = 0
print("You must've mistyped one of the colours, please try again")
counter = counter - 1
continue
else:
inks.append(temp)
multiplier = 10 ** col[inks[2]]
dig1 = str(col[inks[0]])
dig2 = str(col[inks[1]])
digCon = dig1 + dig2
digCon = int(digCon)
resistance = digCon * multiplier
print(f"your resistance is {resistance}")
def five_bands(col): #if a resistor is 6 band, the last 2 bands are tolerance and temperature coefficient
inks = []
counter = 0
while counter < 4:
counter += 1
print(f"please enter band {counter}")
temp = input("enter band colour:")
temp.lower
colours_matching = 0
for key, value in colours.items():
if temp == key:
colours_matching += 1
if colours_matching != 1:
colours_matching = 0
print("You must've mistyped one of the colours, please try again")
counter = counter - 1
continue
else:
inks.append(temp)
multiplier = 10 ** col[inks[3]]
dig1 = str(col[inks[0]])
dig2 = str(col[inks[1]])
dig3 = str(col[inks[2]])
digCon = dig1 + dig2 + dig3
digCon = int(digCon)
resistance = digCon * multiplier
print(f"your resistance is {resistance}")
#main menu:
while True:
while True:
try:
band_num = int(input("Please enter the number of bands your resistor has (excluding tolerance and temperature coefficient):"))
while band_num not in range(3,5):
print("please enter either 3 or 4")
band_num = int(input("Please enter the number of bands your resistor has (excluding tolerance and temperature coefficient):"))
except ValueError:
print("enter an integer!")
continue
else:
break
if band_num == 3:
four_bands(colours)
continue
if band_num == 4:
five_bands(colours)