Skip to content

Commit 04b05ee

Browse files
author
dodo
committed
Beispieltaschenrechner
1 parent 891b52c commit 04b05ee

1 file changed

Lines changed: 92 additions & 0 deletions

File tree

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
u"""
2+
Das folgende Programm ist ein einfacher Taschenrechner.
3+
4+
Nach dem Eingeben zweier Zahlen kann eine Operation ausgewählt werden.
5+
Anschließend wird nach neuen Zahlen gefragt.
6+
"""
7+
8+
# Für die Berechnung der Quadratwurzel wird die math Bibliothek benötigt,
9+
# desweiteren wird zum vorzeitigen Beenden die sys Bibliothek benötigt.
10+
from math import sqrt
11+
import sys
12+
13+
# Zuerst eine Willkommensnachricht
14+
print()
15+
print("Dies ist ein einfacher Taschenrechner.")
16+
print()
17+
18+
# Das Menü wird angezeigt:
19+
print("0: Betrag")
20+
print("1: Summe")
21+
print("2: Produkt")
22+
print("3: Differenz")
23+
print("4: Quotient")
24+
print("5: Modulo Division")
25+
print("6: Quadratwurzel")
26+
print("7: Potenz")
27+
print("8: Fakultät")
28+
print("q: Beenden")
29+
print()
30+
31+
# Das Programm läuft in einer Endlosschleife und wird durch eine entsprechende
32+
# Usereingabe beendet.
33+
while True:
34+
choice = input("Bitte eine Operation auswählen: ")
35+
36+
# Falls der Benutzer ein "q" eingibt soll das Programm beendet werden
37+
if choice.upper() == "Q":
38+
print("Auf Wiedersehen")
39+
sys.exit(0)
40+
41+
x = input("Bitte eine ganze Zahl eingeben: ")
42+
if choice not in ("0", "6", "8"):
43+
# Diese Operationen benötigen mehr als einen Wert
44+
y = input("Bitte eine weitere ganze Zahl eingeben: ")
45+
y = int(y)
46+
47+
# Die Eingabe wird in einen Integer umgewandelt
48+
x = int(x)
49+
50+
if choice == "0":
51+
print("Betrag: |x| =", abs(x)) # Betrag von x
52+
53+
elif choice == "1":
54+
print("Summe: x + y =", x + y) # Summe
55+
56+
elif choice == "2":
57+
print("Produkt: x * y =", x * y) # Produkt
58+
59+
elif choice == "3":
60+
print("Differenz:")
61+
print("x - y =", x - y) # Differenz
62+
print("y - x =", y - x) # Differenz
63+
64+
elif choice == "4":
65+
print("Quotient:")
66+
print("x / y =", x / y) # Quotient
67+
print("y / x =", y / x) # Quotient
68+
69+
elif choice == "5":
70+
print("Rest:")
71+
print("x % y =", x % y) # Modulo Divison
72+
print("y % x =", y % x) # Modulo Divison
73+
74+
elif choice == "6":
75+
print("Quadratwurzel: sqrt(x) =", sqrt(x)) # Quadratwurzel
76+
77+
elif choice == "7":
78+
print("Potenz:")
79+
print("x ^ y =", pow(x, y)) # Potenz
80+
print("y ^ x =", pow(y, x)) # Potenz
81+
82+
elif choice == "8":
83+
tmp = 1
84+
for i in range(2, x + 1):
85+
tmp = tmp * i
86+
print("Fakultät: x! =", str(tmp))
87+
88+
else:
89+
print("Ungültige Eingabe")
90+
continue
91+
92+
print("")

0 commit comments

Comments
 (0)