Skip to content

Commit 891b52c

Browse files
author
dodo
committed
Beispieltaschenrechner
1 parent 50b1274 commit 891b52c

1 file changed

Lines changed: 77 additions & 0 deletions

File tree

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
u"""
2+
Das folgende Programm ist ein einfacher Taschenrechner.
3+
4+
Nach dem Eingeben zweier Zahlen kann eine Operation ausgewählt werden.
5+
"""
6+
7+
# Für die Berechnung der Quadratwurzel wird die math Bibliothek benötigt,
8+
# desweiteren wird zum vorzeitigen Beenden die sys Bibliothek benötigt.
9+
import math
10+
import sys
11+
12+
# Zuerst eine Willkommensnachricht
13+
print()
14+
print("Dies ist ein einfacher Taschenrechner.")
15+
print()
16+
17+
print("Bitte zwei Zahlen eingeben: ")
18+
x = input("Erste Zahl: ")
19+
y = input("Zweite Zahl: ")
20+
21+
# Die beiden Eingaben werden in Zahlen umgewandelt, damit das Programm auch mit
22+
# Fließkommazahlen arbeiten kann, wird der Typ float verwendet.
23+
x = float(x)
24+
y = float(y)
25+
26+
# Zuerst wird ein kleines Menü angezeigt:
27+
print("0: Betrag")
28+
print("1: Summe")
29+
print("2: Produkt")
30+
print("3: Differenz")
31+
print("4: Quotient")
32+
print("5: Modulo Division")
33+
print("6: Quadratwurzel")
34+
print("7: Potenz")
35+
print("q: Beenden")
36+
37+
# Nun wird nach der Auswahl gefragt
38+
choice = input("Bitte eine Operation aussuchen: ")
39+
40+
if choice == "0":
41+
print("|x| =", abs(x)) # Betrag von x
42+
print("|y| =", abs(y)) # Betrag von y
43+
44+
elif choice == "1":
45+
print("x + y =", x + y) # Summe
46+
47+
elif choice == "2":
48+
print("x * y =", x * y) # Produkt
49+
50+
elif choice == "3":
51+
print("x - y =", x - y) # Differenz
52+
print("y - x =", y - x) # Differenz
53+
54+
elif choice == "4":
55+
print("x / y =", x / y) # Quotient
56+
print("y / x =", y / x) # Quotient
57+
58+
elif choice == "5":
59+
print("x % y =", x % y) # Modulo Divison
60+
print("y % x =", y % x) # Modulo Divison
61+
62+
elif choice == "6":
63+
print("sqrt(x) =", math.sqrt(x)) # Quadratwurzel
64+
print("sqrt(y) =", math.sqrt(y)) # Quadratwurzel
65+
66+
elif choice == "7":
67+
print("x ^ y =", pow(x, y)) # Potenz
68+
print("y ^ x =", pow(y, x)) # Potenz
69+
70+
elif choice == "q" or choice == "Q":
71+
# Alternativ: choice.upper() == "Q"
72+
print("Auf Wiedersehen")
73+
sys.exit(0)
74+
75+
else:
76+
print("Falsche Eingabe")
77+
sys.exit(1)

0 commit comments

Comments
 (0)