-
Notifications
You must be signed in to change notification settings - Fork 0
/
schleifen_beispiele.py
214 lines (172 loc) · 4.27 KB
/
schleifen_beispiele.py
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
# Folie 3
num = int(input("How many times should I print the character * ? "))
if num == 1:
print("*")
elif num == 2:
print("**")
elif num == 3:
print("***")
# Folie 6
num = 1
while num < 10:
print(num, end=" ")
num = num + 1
# Folie 7
num = int(input("How many times should I print the character * ? "))
while num > 0:
print("*", end="")
num = num - 1
# Folie 9
secret = 42
guess = int(input("Guess a number in the range 1 to 100: "))
tries = 1
while guess != secret:
print("You tried to guess", tries, "times")
guess = int(input("Guess again: "))
tries += 1
print("You got it! The answer is:", secret)
# Folie 11
num = int(input("How many * ? "))
for i in range(num):
print("*", end="")
# Folie 16
word = input("Word: ")
for i in range(len(word)):
print(word[i], end=" ")
# Folie 17
word = input("Word: ")
for c in word:
print(c, end=" ")
# Folie 20
num = int(input("How many numbers? "))
for i in range(1, num + 1):
for j in range(1, num + 1):
print(i * j, end="\t")
print()
# Folie 21
num = int(input("Number: "))
for i in range(num):
for j in range(num):
print("*", end=" ")
print()
num = int(input("Number: "))
for i in range(num):
print("* " * num)
# Folie 22
num = int(input("Number: "))
for i in range(num):
for j in range(num - i):
print("*", end=" ")
print()
num = int(input("Number: "))
for i in range(num):
print("* " * (num - i))
# Folie 23
number = int(input("Number to check: "))
for i in range(2, number):
if number % i == 0:
print(number, "==", i, "*", number // i)
break
if i == number - 1:
print(number, "is a prime number")
# Folie 24
print("Please enter a number between 1 and 10: ")
while True:
number = int(input("Number: "))
if 1 < number < 10:
break
else:
print("Number should be between 1 and 10")
print("Number entered:", number)
# Folie 25
word = input("Word: ")
counter = 0
for c in word:
if c in "aeiouAEIOU":
continue
counter = counter + 1
print("Number of consonants found:", counter)
# Folie 26
print("Please enter a number between 1 and 10: ")
number = int(input("Number: "))
while not (1 < number < 10):
print("Number should be between 1 and 10")
number = int(input("Number: "))
print("Number entered:", number)
word = input("Word: ")
counter = 0
for c in word:
if c not in "aeiouAEIOU":
counter = counter + 1
print("Number of consonants found:", counter)
# Folie 27
number = int(input("Number to check: "))
for i in range(2, number):
if number % i == 0:
print(number, "==", i, "*", number // i)
break
else:
print(number, "is a prime number")
# Folie 32
num = int(input('Enter an integer: '))
ans = 0
while ans ** 3 < abs(num):
ans = ans + 1
if ans ** 3 != abs(num):
print(num, 'is not a perfect cube')
else:
if num < 0:
ans = -ans
print('Cube root of', num, 'is', ans)
# Folie 34
num = int(input('Enter an integer: '))
for ans in range(0, abs(num) + 1):
if ans ** 3 >= abs(num):
break
if ans ** 3 != abs(num):
print(num, 'is not a perfect cube')
else:
if num < 0:
ans = -ans
print('Cube root of', num, 'is', ans)
# Folie 35
num = float(input('Enter number: '))
epsilon = 0.01
step = epsilon ** 2
num_guesses = 0
ans = 0.0
while abs(ans ** 2 - num) >= epsilon and ans * ans <= num:
ans += step
num_guesses += 1
print('num_guesses =', num_guesses)
if abs(ans ** 2 - num) >= epsilon:
print('Failed on square root of', num)
else:
print(ans, 'is close to square root of', num)
# Folie 40
num = float(input('Enter number: '))
epsilon = 0.001
num_guesses = 0
low = 0.0
high = max(1.0, num)
ans = (high + low) / 2.0
while abs(ans ** 2 - num) >= epsilon:
print('low =', low, 'high =', high, 'ans =', ans)
num_guesses += 1
if ans ** 2 < num:
low = ans
else:
high = ans
ans = (high + low) / 2.0
print('num_guesses =', num_guesses)
print(ans, 'is close to square root of', num)
# Folie 43
num = float(input('Enter number: '))
epsilon = 0.01
num_guesses = 0
guess = num / 2.0
while abs(guess * guess - num) >= epsilon:
guess = guess - (((guess ** 2) - num) / (2 * guess))
num_guesses += 1
print('num_guesses =', num_guesses)
print('Square root of', num, 'is about', guess)