mirror of
https://github.com/sys-32Dev/CS1010.git
synced 2026-08-08 20:05:59 +00:00
add Lab 9
Add Lab 9
This commit is contained in:
32
lab9/Ex1-For-Loop-lab.py
Normal file
32
lab9/Ex1-For-Loop-lab.py
Normal file
@@ -0,0 +1,32 @@
|
||||
# Edwin Hui
|
||||
x = 2
|
||||
for x in range(1,16):
|
||||
print(x, end=" ")
|
||||
print(" ")
|
||||
# prints 1-15 on one line
|
||||
print("----------------")
|
||||
x = 10
|
||||
for x in range(10,26):
|
||||
print(x, end=" ")
|
||||
print(" ")
|
||||
# prints 10-25 on one line
|
||||
print("----------------")
|
||||
x = 7
|
||||
for x in range(7,30, 4):
|
||||
print(x, end=" ")
|
||||
print(" ")
|
||||
# prints 7-29 by 4 on one line
|
||||
print("----------------")
|
||||
x = 23
|
||||
for x in range(23, 7, -3):
|
||||
print(x, end=" ")
|
||||
print(" ")
|
||||
# prints 23-8 by subtracting 3 every count down
|
||||
print("----------------")
|
||||
x = range(2, 22, 2)
|
||||
y = 0
|
||||
for y in x:
|
||||
print(y, end=" ")
|
||||
print(" ")
|
||||
# prints 2-20 by 2 on one line
|
||||
print("----------------")
|
||||
37
lab9/Ex2-For-Loop-lab.py
Normal file
37
lab9/Ex2-For-Loop-lab.py
Normal file
@@ -0,0 +1,37 @@
|
||||
# Edwin Hui
|
||||
my_list = ['earth', 'exciting', 'day', 'laptop', 'electrochemical', 'blue', 'up']
|
||||
print("Run #1")
|
||||
longest_word = ""
|
||||
for word in my_list:
|
||||
print(word, end = " ")
|
||||
print(len(word))
|
||||
# defines my_list as a list of words, then prints each word and its length
|
||||
if len(word) > len(longest_word):
|
||||
longest_word = word
|
||||
print("The longest word is:", longest_word)
|
||||
# if the length of the word is greater than the length of the longest word, then the longest word is the word
|
||||
print("--------------------")
|
||||
my_list = ['indistinguishable', 'acorn', 'soap', 'ladder', 'cheese', 'disillusioned', 'pit', 'monday', 'placeholder', 'teleconference']
|
||||
print("Run #2")
|
||||
longest_word = ""
|
||||
for word in my_list:
|
||||
print(word, end = " ")
|
||||
print(len(word))
|
||||
# defines my_list as a list of words, then prints each word and its length
|
||||
if len(word) > len(longest_word):
|
||||
longest_word = word
|
||||
print("The longest word is:", longest_word)
|
||||
# if the length of the word is greater than the length of the longest word, then the longest word is the word
|
||||
print("--------------------")
|
||||
my_list = ['fixture', 'dangerous', 'humanitarianism', 'trip', 'orange', 'cat', 'triskaidekaphobia', 'nominal', 'and', 'atomic']
|
||||
print("Run #3")
|
||||
longest_word = ""
|
||||
for word in my_list:
|
||||
print(word, end = " ")
|
||||
print(len(word))
|
||||
# defines my_list as a list of words, then prints each word and its length
|
||||
if len(word) > len(longest_word):
|
||||
longest_word = word
|
||||
print("The longest word is:", longest_word)
|
||||
# if the length of the word is greater than the length of the longest word, then the longest word is the word
|
||||
print("--------------------")
|
||||
43
lab9/Ex3-For-Loop-lab.py
Normal file
43
lab9/Ex3-For-Loop-lab.py
Normal file
@@ -0,0 +1,43 @@
|
||||
# Edwin Hui
|
||||
print("Run 1")
|
||||
my_list = [4, 76, 2, 234, 9, 71]
|
||||
print("input list:", end=" ")
|
||||
for num in my_list:
|
||||
print(num, end=", ")
|
||||
# defines my_list as a list of numbers, then prints each number
|
||||
total = sum(my_list)
|
||||
average = total / len(my_list)
|
||||
largest = max(my_list)
|
||||
print("\nSum:", total)
|
||||
print("Average:", average)
|
||||
print("Largest number:", largest)
|
||||
# finds and prints the sum, average, and largest number of the list
|
||||
print("--------------------")
|
||||
print("Run 2")
|
||||
my_list = [3, -17, 99, -32, 321, 62, 27]
|
||||
print("input list:", end=" ")
|
||||
for num in my_list:
|
||||
print(num, end=", ")
|
||||
# defines my_list as a list of numbers, then prints each number
|
||||
total = sum(my_list)
|
||||
average = total / len(my_list)
|
||||
largest = max(my_list)
|
||||
print("\nSum:", total)
|
||||
print("Average:", average)
|
||||
print("Largest number:", largest)
|
||||
# finds and prints the sum, average, and largest number of the list
|
||||
print("--------------------")
|
||||
print("Run 3")
|
||||
my_list = [1899, 1978, 23, 0, 23, 37, 237, 8, 2, -77, 31]
|
||||
print("input list:", end=" ")
|
||||
for num in my_list:
|
||||
print(num, end=", ")
|
||||
# defines my_list as a list of numbers, then prints each number
|
||||
total = sum(my_list)
|
||||
average = total / len(my_list)
|
||||
largest = max(my_list)
|
||||
print("\nSum:", total)
|
||||
print("Average:", average)
|
||||
print("Largest number:", largest)
|
||||
# finds and prints the sum, average, and largest number of the list
|
||||
print("--------------------")
|
||||
32
lab9/Ex4-For-Loop-lab.py
Normal file
32
lab9/Ex4-For-Loop-lab.py
Normal file
@@ -0,0 +1,32 @@
|
||||
#Edwin Hui
|
||||
height = 5
|
||||
for i in range(height):
|
||||
for j in range(i + 1):
|
||||
print("o", end="")
|
||||
print()
|
||||
# prints a triangle by adding one "o" every line until it reaches the height
|
||||
print("--------------------")
|
||||
height = 6
|
||||
for i in range(height):
|
||||
for j in range(height - i):
|
||||
print("o", end="")
|
||||
print()
|
||||
# prints a triangle by subtracting one "o" every line until it reaches the height
|
||||
print("--------------------")
|
||||
height = int(input("Enter the height of the triangle: "))
|
||||
for i in range(height):
|
||||
for j in range(i):
|
||||
print("o", end="")
|
||||
print()
|
||||
for i in range(height):
|
||||
for j in range(height - i):
|
||||
print("o", end="")
|
||||
print()
|
||||
# prints a trinagle by adding one "o" every line until it reaches the user definde height, then subtracting one "o" every line
|
||||
print("--------------------")
|
||||
height = int(input("Enter the height of the triangle: "))
|
||||
for i in range(height):
|
||||
print(" "*(height-i-1) + "o"*(2*i+1))
|
||||
# prints an equalateral triangle by printing spaces proportional to the height, then printing "o" 2 times var i + 1
|
||||
|
||||
|
||||
Reference in New Issue
Block a user