-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnumshift.py
More file actions
78 lines (62 loc) · 2.01 KB
/
numshift.py
File metadata and controls
78 lines (62 loc) · 2.01 KB
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
#!/usr/bin/env python3
import sys
BASES = {
"1": ("bin", 2),
"2": ("oct", 8),
"3": ("dec", 10),
"4": ("hex", 16),
}
def ask_base(prompt):
while True:
print(prompt)
print("1. bin\n2. oct\n3. dec\n4. hex\nq. quit")
choice = input("base: ").strip()
if choice == "q":
print("\nBye :)")
sys.exit(0)
if choice in BASES:
return choice
print("\nWrong base!\n")
def convert(value: str, from_base: int, to_base: int):
try:
n = int(value, from_base)
except ValueError:
return None
if to_base == 2:
return bin(n)[2:]
elif to_base == 8:
return oct(n)[2:]
elif to_base == 10:
return str(n)
elif to_base == 16:
return hex(n)[2:].upper()
def main():
for i in range(10):
in_choice = ask_base("Choose an input base")
print("\n")
out_choice = ask_base("Choose an output base")
if in_choice == out_choice:
print("\nYou chose the same base twice!\n")
continue
in_name, in_base = BASES[in_choice]
out_name, out_base = BASES[out_choice]
print(f"\n{in_name} > {out_name}")
raw = input(f"Enter {in_name} number(s) separated by spaces or commas: ")
numbers = [x.strip() for x in raw.replace(",", " ").split() if x.strip()]
result_list = []
print("\n===== Conversion Results =====")
for num in numbers:
result = convert(num, in_base, out_base)
if result is None:
print(f"{num}: is not a {in_name} number")
continue
else:
print(f"{in_name}({num}) → {out_name}: {result}")
result_list.append(result)
print("\n")
if result_list:
print(f"List separed by spaces: {' '.join(result_list)}")
print(f"List separed by commas: {', '.join(result_list)}")
print("==============================\n")
if __name__ == "__main__":
main()