-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathlec2_branch_loops.py
127 lines (107 loc) · 2.81 KB
/
lec2_branch_loops.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
###################
## EXAMPLE: strings
###################
#hi = "hello there"
#name = "ana"
#greet = hi + name
#print(greet)
#greeting = hi + " " + name
#print(greeting)
#silly = hi + (" " + name)*3
#print(silly)
####################
## EXAMPLE: output
####################
#x = 1
#print(x)
#x_str = str(x)
#print("my fav number is", x, ".", "x=", x)
#print("my fav number is", x_str + "." + "x=" + x_str)
#print("my fav number is" + x_str + "." + "x=" + x_str)
####################
## EXAMPLE: input
####################
#text = input("Type anything... ")
#print(5*text)
#num = int(input("Type a number... "))
#print(5*num)
####################
## EXAMPLE: conditionals/branching
####################
#x = float(input("Enter a number for x: "))
#y = float(input("Enter a number for y: "))
#if x == y:
# print("x and y are equal")
# if y != 0:
# print("therefore, x / y is", x/y)
#elif x < y:
# print("x is smaller")
#elif x > y:
# print("y is smaller")
#print("thanks!")
####################
## EXAMPLE: remainder
####################
#num = int(input("Enter a number: "))
#if num % 2 == 0:
# print("number is even")
#else:
# print("number is odd")
####################
## EXAMPLE: while loops
## Try expanding this code to show a sad face if you go right
## twice and flip the table any more times than that.
## Hint: use a counter
####################
#n = input("You are in the Lost Forest\n****************\n****************\n :)\n****************\n****************\nGo left or right? ")
#while n == "right" or n == "Right":
# n = input("You are in the Lost Forest\n****************\n****** ***\n (╯°□°)╯︵ ┻━┻\n****************\n****************\nGo left or right? ")
#print("\nYou got out of the Lost Forest!\n\o/")
#n = 0
#while n < 5:
# print(n)
# n = n+1
####################
## EXAMPLE: for loops
####################
#for n in range(5):
# print(n)
#
#mysum = 0
#for i in range(10):
# mysum += i
#print(mysum)
#
#mysum = 0
#for i in range(7, 10):
# mysum += i
#print(mysum)
#
#mysum = 0
#for i in range(5, 11, 2):
# mysum += i
# if mysum == 5:
# break
# mysum += 1
#print(mysum)
####################
## EXAMPLE: perfect squares
####################
#ans = 0
#neg_flag = False
#x = int(input("Enter an integer: "))
#if x < 0:
# neg_flag = True
#while ans**2 < x:
# ans = ans + 1
#if ans**2 == x:
# print("Square root of", x, "is", ans)
#else:
# print(x, "is not a perfect square")
# if neg_flag:
# print("Just checking... did you mean", -x, "?")
####################
## TEST YOURSELF!
## Modify the perfect squares example to print
## imaginary perfect sqrts if given a negative num.
####################