Created
July 10, 2022 11:55
-
-
Save HMAnnex/1c77b288e0493a621dcf7b29c62d9bb8 to your computer and use it in GitHub Desktop.
Module 3 Graded Assessment : Crash course Python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Fill in the blanks of this code to print out the numbers 1 through 7. | |
number = 1 | |
while number <= 7: | |
print(number, end=" ") | |
number += 1 | |
#The show_letters function should print out each letter of a word on a separate line. Fill in the blanks to make that happen. | |
def show_letters(word): | |
for i in word: | |
print(i) | |
show_letters("Hello") | |
# Should print one line per letter | |
#Complete the function digits(n) that returns how many digits the number has. | |
#For example: 25 has 2 digits and 144 has 3 digits. | |
#Tip: you can figure out the digits of a number by dividing it by 10 once per digit until there are no digits left. | |
def digits(n): | |
count = 0 | |
if n == 0: | |
return 1 | |
while (n > 0): | |
count += 1 | |
n=n//10 | |
return count | |
print(digits(25)) # Should print 2 | |
print(digits(144)) # Should print 3 | |
print(digits(1000)) # Should print 4 | |
print(digits(0)) # Should print 1 | |
# This function prints out a multiplication table (where each number is the result of multiplying the | |
# first number of its row by the number at the top of its column). Fill in the blanks so that calling | |
# multiplication_table(1, 3) will print out: | |
# 1 2 3 | |
# 2 4 6 | |
# 3 6 9 | |
def multiplication_table(start, stop): | |
for x in range(start, stop+1): | |
for y in range(start, stop+1): | |
print(str(x*y), end=" ") | |
print() | |
# The counter function counts down from start to stop when start is bigger than stop, | |
# and counts up from start to stop otherwise. Fill in the blanks to make this work correctly. | |
def counter(start, stop): | |
x = start | |
if start > stop: | |
return_string = "Counting down: " | |
while x >= stop: | |
return_string += str(x) | |
x = x-1 | |
if start != stop: | |
return_string += "," | |
else: | |
return_string = "Counting up: " | |
while x <= stop: | |
return_string += str(x) | |
x = x + 1 | |
if start != stop: | |
return_string += "," | |
return return_string.rstrip(',') | |
print(counter(1, 10)) # Should be "Counting up: 1,2,3,4,5,6,7,8,9,10" | |
print(counter(2, 1)) # Should be "Counting down: 2,1" | |
print(counter(5, 5)) # Should be "Counting up: 5" | |
# The even_numbers function returns a space-separated string of all positive numbers that are divisible by 2, | |
# up to and including the maximum that's passed into the function. For example, even_numbers(6) returns “2 4 6”. | |
# Fill in the blank to make this work. | |
def even_numbers(maximum): | |
return_string = "" | |
for x in range(1, maximum+1): | |
num = x%2 | |
if num == 0: | |
return_string += str(x) + " " | |
return return_string.strip() | |
print(even_numbers(6)) # Should be 2 4 6 | |
print(even_numbers(10)) # Should be 2 4 6 8 10 | |
print(even_numbers(1)) # No numbers displayed | |
print(even_numbers(3)) # Should be 2 | |
print(even_numbers(0)) # No numbers displayed | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment