Attachment 'lec2.py'
Download 1 ###################
2 ## EXAMPLE: strings
3 ###################
4 #hi = "hello there"
5 #name = "ana"
6 #greet = hi + name
7 #print(greet)
8 #greeting = hi + " " + name
9 #print(greeting)
10 #silly = hi + (" " + name)*3
11 #print(silly)
12
13 ####################
14 ## EXAMPLE: output
15 ####################
16 #x = 1
17 #print(x)
18 #x_str = str(x)
19 #print("my fav number is", x, ".", "x=", x)
20 #print("my fav number is", x_str + "." + "x=" + x_str)
21 #print("my fav number is" + x_str + "." + "x=" + x_str)
22
23
24 ####################
25 ## EXAMPLE: input
26 ####################
27 #text = input("Type anything... ")
28 #print(5*text)
29 #num = int(input("Type a number... "))
30 #print(5*num)
31
32
33 ####################
34 ## EXAMPLE: conditionals/branching
35 ####################
36 #x = float(input("Enter a number for x: "))
37 #y = float(input("Enter a number for y: "))
38 #if x == y:
39 # print("x and y are equal")
40 # if y != 0:
41 # print("therefore, x / y is", x/y)
42 #elif x < y:
43 # print("x is smaller")
44 #elif x > y:
45 # print("y is smaller")
46 #print("thanks!")
47
48
49
50 ####################
51 ## EXAMPLE: remainder
52 ####################
53 #num = int(input("Enter a number: "))
54 #if num % 2 == 0:
55 # print("number is even")
56 #else:
57 # print("number is odd")
58
59
60 ####################
61 ## EXAMPLE: while loops
62 ## Try expanding this code to show a sad face if you go right
63 ## twice and flip the table any more times than that.
64 ## Hint: use a counter
65 ####################
66 #n = input("You are in the Lost Forest\n****************\n****************\n :)\n****************\n****************\nGo left or right? ")
67 #while n == "right" or n == "Right":
68 # n = input("You are in the Lost Forest\n****************\n****** ***\n (╯°□°)╯︵ ┻━┻\n****************\n****************\nGo left or right? ")
69 #print("\nYou got out of the Lost Forest!\n\o/")
70
71
72
73 #n = 0
74 #while n < 5:
75 # print(n)
76 # n = n+1
77
78
79 ####################
80 ## EXAMPLE: for loops
81 ####################
82 #for n in range(5):
83 # print(n)
84 #
85 #mysum = 0
86 #for i in range(10):
87 # mysum += i
88 #print(mysum)
89 #
90 #mysum = 0
91 #for i in range(7, 10):
92 # mysum += i
93 #print(mysum)
94 #
95 #mysum = 0
96 #for i in range(5, 11, 2):
97 # mysum += i
98 # if mysum == 5:
99 # break
100 # mysum += 1
101 #print(mysum)
102
103
104
105 ####################
106 ## EXAMPLE: perfect squares
107 ####################
108 #ans = 0
109 #neg_flag = False
110 #x = int(input("Enter an integer: "))
111 #if x < 0:
112 # neg_flag = True
113 #while ans**2 < x:
114 # ans = ans + 1
115 #if ans**2 == x:
116 # print("Square root of", x, "is", ans)
117 #else:
118 # print(x, "is not a perfect square")
119 # if neg_flag:
120 # print("Just checking... did you mean", -x, "?")
121
122
123 ####################
124 ## TEST YOURSELF!
125 ## Modify the perfect squares example to print
126 ## imaginary perfect sqrts if given a negative num.
127 ####################
Attached Files
To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.You are not allowed to attach a file to this page.