-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkalkulator.py
More file actions
100 lines (79 loc) · 2.2 KB
/
kalkulator.py
File metadata and controls
100 lines (79 loc) · 2.2 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# Kalkulator
options = {
'1': "dodawanie",
'2': "odejmowanie",
'3': "mnożenie",
'4': "dzielenie",
'5': "potęgowanie",
'6': "pierwiastkowanie"
}
def checkIfNumber(arg):
try:
arg = arg.replace(",", ".")
arg = float(arg)
except(ValueError):
print("Błędne dane wejściowe")
quit()
return float(arg)
def checkBInRoot(arg, bInRoot):
bInRoot = checkIfNumber(bInRoot)
if(arg < 0 and bInRoot % 2 == 0):
print("Nie można wyciągnąć parzystego pierwiastka z ujemnej liczby")
quit()
return bInRoot
def checkBInPow(arg, bInPow):
bInPow = checkIfNumber(bInPow)
print((1/bInPow))
if(arg < 0 and (1/bInPow) % 2 == 0):
print("Nie można wyciągnąć parzystego pierwiastka z ujemnej liczby")
quit()
return bInPow
print("Wybierz działanie:\n")
for key in options:
print(f"{key}. {options[key]}")
opt = input("\n")
if(not(options.get(opt))):
quit()
print(options.get(opt))
a = b = 0
match(opt):
case '1'|'2'|'3'|'4':
a = input("Podaj a: ")
a = checkIfNumber(a)
b = input("Podaj b: ")
b = checkIfNumber(b)
match(opt):
case '1':
print(f"{a} + {b} = {a + (b)}")
case '2':
print(f"{a} - {b} = {a - (b)}")
case '3':
print(f"{a} * {b} = {(round(a * (b) * 100))/100}")
case '4':
if(b == 0):
print("Nie można dzielić przez 0")
quit()
print(f"{a} / {b} = {(round(a / (b) * 100))/100}")
case '5':
a = input("Podaj podstawę potęgi: ")
a = checkIfNumber(a)
b = input("Podaj wykładnik potęgi: ")
b = checkBInPow(a, b)
printNegative = False
if(a < 0):
a = -a
printNegative = True
printMinus = "-" if printNegative else ""
print(f"{printMinus}{a} ^ {b} = {printMinus}{(round(a ** (b) * 100))/100}")
case '6':
a = input("Podaj liczbę z której chcesz wyciągnąć pierwiastek: ")
a = checkIfNumber(a)
b = input("Podaj stopień pierwiastka: ")
b = checkBInRoot(a, b)
printNegative = False
if(a < 0):
a = -a
printNegative = True
printMinus = "-" if printNegative else ""
print(f"Pierwiastek {b} stopnia z {printMinus}{a} = " +
f"{printMinus}{(round(a ** (1/(b)) * 100))/100}")