4. Some examples¶
Make a program that reads a vector of 5 integers and displays the vector.
8
vector = []
i = 1
while i <= 5:
n = int(input("Enter a number: "))
vector.append(n)
i = i + 1
print("Read vector:", vector)
Activity: 4.1 ActiveCode (ac_l17_4a_en)
Make a program that reads a vector of ten real numbers and displays them in reverse order.
11
vector = []
i = 1
while i <= 10:
n = float(input("Enter a number: "))
vector.append(n)
i += 1
i = 9
while i >= 0:
print(vector[i])
i -= 1
Activity: 4.2 ActiveCode (ac_l17_4b_en)
Make a program that reads four grades, displays the grades, and the average on the screen.
14
grades = []
i = 1
while i <= 4:
n = float(input("Grade: "))
grades.append(n)
i += 1
suma = 0
i = 0
while i <= 3:
suma += grades[i]
i += 1
print("Grades:", grades)
print("Average: %4.2f" % (suma / 4))
Activity: 4.3 ActiveCode (ac_l17_4c_en)
Another way of doing the same thing.
11
grades = []
i = 1
suma = 0
while i <= 4:
n = float(input("Grade: "))
grades.append(n)
suma += n
i += 1
print("Grades:", grades)
print("Average: %4.2f" % (suma / 4))
Activity: 4.4 ActiveCode (ac_l17_4d_en)
Make a program that reads a vector of 10 lowercase characters, and tells how many consonants were read.
13
letters = []
i = 1
while i <= 10:
letters.append(input("Letter: "))
i += 1
i = 0
cont = 0
while i <= 9:
if letters[i] not in "aeiou":
cont += 1
i += 1
print("%d consonants were read" % cont)
Activity: 4.5 ActiveCode (ac_l17_4e_en)
You have attempted 1 of 6 activities on this page