total = 0
index = 1
while index <= 5:
print('adding ' + str(index))
total = total + index
index = index + 1
print(total)
adding 1
adding 2
adding 3
adding 4
adding 5
15
while
loopsindex
variable:
temporary variable
for aggregation:
while
loops with aggregationAdding numbers from 1 to 5:
while
loops tableStart with total = 0
, index = 1
:
index <= 5 |
total = total + index | index = index + 1 |
---|---|---|
True | total = 0 + 1 | index = 1 + 1 |
True | total = 1 + 2 | index = 2 + 1 |
True | total = 3 + 3 | index = 3 + 1 |
True | total = 6 + 4 | index = 4 + 1 |
True | total = 10 + 5 | index = 5 + 1 |
False | - | - |
Its name is sum_all
and takes two numeric arguments: low
and high
. It runs a loop that iterates through the values low
and high
summing all values. It returns the sum of all values between low
and high
.
HINT: Create a variable that will aggregate the sum. Use while
(set the index as the low
before the loop, use index in the while
condition, update the index inside the loop).
Write a function called count_vowels
that takes a string as argument, and returns an integer with the number of vowels in the string.
HINT: use in
to determine if a character is a vowel. The vowels are "a"
,"e"
,"i"
,"o"
, and "u"
.
Its name is factorial
and takes a numeric argument number
. It returns the factorial of number
. The factorial of a number is the product of that number and every number that comes before it, down to one.
For example, factorial of 4: 1 * 2 * 3 * 4 = 24
.
Name your file factorial.py
and submit to Gradescope.
power
base
and exp
base
to the power of exp
**
operator, use a while
loop (define an index
before the loop, use index
in the while
condition, change index
inside the loop)Write more test cases for this function.
vowels_only
string
argumentstring
argumentindex
before the loop, use index in the while
condition, change index
inside the loop)