-
Notifications
You must be signed in to change notification settings - Fork 0
/
cat.py
84 lines (58 loc) · 1.57 KB
/
cat.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
# ## Print meow 3x
# print("meow")
# print("meow")
# print("meow")
# ## How could you print 'meow' three times
# ## 'while' statement to express a loop
# i = 3
# while i != 0:
# print("meow")
# i = i - 1 # This allows for the loop to stop after printing 'meow' 3 times
# ## Similarly you could do
# i = 0
# while i < 3:
# print("meow")
# i = i + 1
# ## Exact same code above but using '+='
# i = 0
# while i < 3:
# print("meow")
# i += 1
# ## 'for' loops
# for i in [0, 1, 2]: # Understand that here you're saying for a variabel 'i' in the list '0-2', it will print meow for each number in the list
# print("meow")
# ## Similarly and better code from above, using 'range'
# for i in range(3):
# print("meow")
# ## You can also use ' _ ' instead of specific variable
# for _ in range(3):
# print("meow")
# ## What about using '*' to print 'meow' 3 times?
# print("meow\n" * 3, end="")
# ## Asking the user to input a number of times to 'meow'?
# while True:
# n = int(input("What's n? "))
# if n > 0:
# break
# for _ in range(n):
# print("meow")
# ## Another approach; however, this is hard coding to print 'meow' 3 times
# def main():
# meow(3)
# def meow(n):
# for _ in range(n):
# print("meow")
# main()
## Editng the code above to not hard code to print 'meow' 3 times
def main():
number = get_number()
meow(number)
def get_number():
while True:
n = int(input("What's n? "))
if n > 0:
return n
def meow(n):
for _ in range(n):
print("meow")
main()