Skip to content

Commit 0f53601

Browse files
committed
Lab 4
1 parent a8188ca commit 0f53601

File tree

13 files changed

+151
-0
lines changed

13 files changed

+151
-0
lines changed

L4-1.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# L4_1.​ Write a program that asks for user input of a number.
2+
# The program should ask the user to input numbers until the user
3+
# wants to stop. The number must be between 1 and 100 and the program
4+
# stops if the user inputs a 0. The values a user inputs should be stored
5+
# in a Python list and when the user finishes entering numbers and exits
6+
# the list should be printed.
7+
8+
theList = list()
9+
10+
while True:
11+
user_input = int(input('Enter a number between 1 and 100, or 0 to end: '))
12+
if user_input == 0:
13+
break
14+
if (user_input < 0) or (user_input > 100):
15+
print('Invalid, input must be between 1 and 100!')
16+
continue
17+
theList.append(user_input)
18+
print('theList: ', theList)

L4-10.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# L4_10.​ Describe the difference between the following two code scripts:
2+
3+
# script A - range(5) starts at 0 and goes to 4, the count of elements is 5
4+
5+
for i in range(5):
6+
print(i)
7+
8+
print()
9+
10+
# script b - range(5) starts at 1 and goes to 4, the count of elements is 4
11+
12+
for i in range(1, 5):
13+
print(i)

L4-11.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# L4_11. After executing the following Python script observe the results:
2+
3+
for each in range(1980, 2020):
4+
if each % 4 == 0:
5+
result = True
6+
else:
7+
result = False
8+
9+
print(each, '\tLeap Year?\t ', result)

L4-12.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# L4_12
2+
3+
list = [each for each in range(1980, 2020)]
4+
print(list)
5+
for each in list:
6+
if each % 4 == 0:
7+
result = True
8+
else:
9+
result = False
10+
print(each, '\t ', result)

L4-12alternate.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Rewrite of L14-12
2+
3+
list = [each for each in range(1980, 2020)]
4+
print(list)
5+
for each in list:
6+
result = True if (each % 4 == 0) else False
7+
print(each, '\t ', result)

L4-2.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# L4_2.​ Write a Python function that prints ‘Function Reached’.
2+
3+
4+
def thePrint():
5+
print('Function Reached')
6+
7+
8+
thePrint()

L4-3.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# L4_3.​ Write a Python function that receives a value (parameter)
2+
# and prints what was passed (parameter) to the function.
3+
4+
def thePrint(parameter):
5+
print(parameter)
6+
7+
8+
thePrint('Test one.')
9+
thePrint('Test two.')

L4-4.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# L4_4.​ The mathematical integer division is used to calculate how many times
2+
# a value is divisible by another value.
3+
# As an example, 100 integer division 25 is 4
4+
# (Python represents this calculation as 100//25)
5+
# and 102 integer division 25 is 4. # Any remainder is ignored (see L4_5).
6+
# Write a function that takes two values as parameters and returns
7+
# the calculation of integer division from the two values.
8+
9+
10+
def integer_division(a, b):
11+
''' divide a by b, return the integer result '''
12+
return a // b
13+
14+
15+
print('Divide 100 by 25, result is: ', integer_division(100, 25))
16+
print('Divide 102 by 25, result is: ', integer_division(102, 25))

L4-5.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# The mathematical modulo is used to calculate the remainder of the
2+
# integer division. As an example, 102%25 is 2.
3+
# Write a function that takes two values as parameters and returns
4+
# the calculation of a modulo from the two values.
5+
6+
def mathematical_modulo(a, b):
7+
''' divide a by b, return the remainder '''
8+
return a % b
9+
10+
11+
print('Divide 100 by 25, remainder is: ', mathematical_modulo(100, 25))
12+
print('Divide 102 by 25, remainder is: ', mathematical_modulo(102, 25))

L4-6.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# L4_6.​ Create a list and then sort the list.
2+
3+
theList = list()
4+
5+
while True:
6+
user_input = int(input('Enter a number between 1 and 100, or 0 to end: '))
7+
if user_input == 0:
8+
break
9+
if (user_input < 0) or (user_input > 100):
10+
print('Invalid, input must be between 1 and 100!')
11+
continue
12+
theList.append(user_input)
13+
14+
print('theList unsorted: ', theList)
15+
theList.sort()
16+
print('theList sorted: ', theList)

0 commit comments

Comments
 (0)