-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathcalculator02_if.py
More file actions
77 lines (60 loc) · 1.91 KB
/
Copy pathcalculator02_if.py
File metadata and controls
77 lines (60 loc) · 1.91 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
"""
Das folgende Programm ist ein einfacher Taschenrechner.
Nach dem Eingeben zweier Zahlen kann eine Operation ausgewählt werden.
"""
# Für die Berechnung der Quadratwurzel wird die math Bibliothek benötigt,
# des Weiteren wird zum vorzeitigen Beenden die sys Bibliothek benötigt.
import math
import sys
# Zuerst eine Willkommensnachricht
print()
print("Dies ist ein einfacher Taschenrechner.")
print()
print("Bitte zwei Zahlen eingeben: ")
x = input("Erste Zahl: ")
y = input("Zweite Zahl: ")
# Die beiden Eingaben werden in Zahlen umgewandelt, damit das Programm auch mit
# Fließkommazahlen arbeiten kann, wird der Typ float verwendet.
x = float(x)
y = float(y)
# Zuerst wird ein kleines Menü angezeigt:
print("0: Betrag")
print("1: Summe")
print("2: Produkt")
print("3: Differenz")
print("4: Quotient")
print("5: Modulo Division")
print("6: Quadratwurzel")
print("7: Potenz")
print("q: Beenden")
# Nun wird nach der Auswahl gefragt
choice = input("Bitte eine Operation aussuchen: ")
if choice == "0":
print("|x| =", abs(x)) # Betrag von x
print("|y| =", abs(y)) # Betrag von y
elif choice == "1":
print("x + y =", x + y) # Summe
elif choice == "2":
print("x * y =", x * y) # Produkt
elif choice == "3":
print("x - y =", x - y) # Differenz
print("y - x =", y - x) # Differenz
elif choice == "4":
print("x / y =", x / y) # Quotient
print("y / x =", y / x) # Quotient
elif choice == "5":
print("x % y =", x % y) # Modulo Division
print("y % x =", y % x) # Modulo Division
elif choice == "6":
print("sqrt(x) =", math.sqrt(x)) # Quadratwurzel
print("sqrt(y) =", math.sqrt(y)) # Quadratwurzel
elif choice == "7":
print("x ^ y =", pow(x, y)) # Potenz
print("y ^ x =", pow(y, x)) # Potenz
elif choice == "q" or choice == "Q":
# Alternativ: choice.upper() == "Q"
print("Auf Wiedersehen")
sys.exit(0)
else:
print("Falsche Eingabe")
sys.exit(1)