-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathloops.py
More file actions
executable file
·86 lines (64 loc) · 1.75 KB
/
loops.py
File metadata and controls
executable file
·86 lines (64 loc) · 1.75 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
#!/usr/bin/env python3
# @file: loops.py
# @auth: Sprax Lines
# @date: 2016-06-07 16:33:12 Tue 07 Jun
# loops.py
for j in range(10):
print(j, end=',')
print()
k = 9
while k >= 0:
print(k, end=' ')
k = k-1
print('\n')
for n in range(2, 10):
for x in range(2, n):
if n % x == 0:
print(n, 'equals', x, '*', n//x)
break
else:
# loop fell through without finding a factor
print(n, 'is a prime number')
# fibonacci_generator
def fib_generate(n):
a, b, x = 0, 1, 0
while x < n:
yield a
a, b = b, a + b
x = x+1
def fib(n): # write Fibonacci series up to n
"""Print a Fibonacci series up to n."""
a, b = 0, 1
while a < n:
print(a, end=' ')
a, b = b, a+b
print()
fib(2000)
print(fib(0))
def printFibGen(n): # write Fibonacci series up to n
"""Print a Fibonacci series up to n."""
print([y for y in fib_generate(n)])
printFibGen(18)
def throw_io_error():
raise IOError('refusenik user')
def constant_factory(value):
return lambda: value
def ask_yes_no(prompt, retries=3, complaint='Yes or no, please!', default_function=constant_factory(False)):
while True:
answer = input(prompt)
yesno = answer.lower()
if yesno.lower() in ('y', 'ye', 'yep', 'yes'):
return True
if yesno in ('n', 'no', 'nop', 'nope'):
return False
retries = retries - 1
if retries <= 0:
return default_function()
print(complaint)
def ask_you_got_it():
if ask_yes_no("You got it? ", 2, "Give it a yes or no.", default_function=throw_io_error):
print("You got it!")
else:
print("You don't got it.")
if __name__ == '__main__':
ask_you_got_it()