forked from ahmedkareem999/MITx-6.00.1x
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwhile.py
59 lines (45 loc) · 1.13 KB
/
while.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
'''
Week-1: While Exercise -1
In this problem you'll be given a chance to practice writing some while loops.
1. Convert the following into code that uses a while loop.
print 2
prints 4
prints 6
prints 8
prints 10
prints Goodbye!
'''
n = 2
while ( n >= 2 and n <=10):
print(n)
n += 2
print("Goodbye!")
'''
Week1: While Exercise-2
2. Convert the following into code that uses a while loop.
prints Hello!
prints 10
prints 8
prints 6
prints 4
prints 2
'''
n = 10
print ("Hello!")
while n <= 10 and n >= 2:
print (n)
n -= 2
'''
Week1:While exercise-3
3. Write a while loop that sums the values 1 through end, inclusive. end is a variable that we define for you. So, for example, if we define end to be 6, your code should print out the result:
21
which is 1 + 2 + 3 + 4 + 5 + 6.
For problems such as these, do not include input statements or define variables we will provide for you. Our automating testing will provide values so write your code in the following box assuming these variables are already defined.
'''
val1 = 0
val2 = 0
while val1 <= end:
val1 += 1
val2 = val2 + val1
if val1 == end:
print(val2)