Skip to content

Instantly share code, notes, and snippets.

@IvanFrecia
Created April 29, 2021 22:34
Show Gist options
  • Save IvanFrecia/f0bca25b8e81670d96de270d7dfeaa68 to your computer and use it in GitHub Desktop.
Save IvanFrecia/f0bca25b8e81670d96de270d7dfeaa68 to your computer and use it in GitHub Desktop.
Coursera Python 3 Programming Specialization - Course 2 - Week 3 - Assessment: Functions - course_2_assessment_4
# 1)Write a function called int_return that takes an integer as input and returns the same integer.
# Answer:
def int_return(n):
return int(n)
print(int_return(2.5)) # This line is not needed
# 2) Write a function called add that takes any number as its input and returns that sum with 2 added.
# Answer:
def add(n):
return float(n + 2)
print(add(5.37)) # This line is not needed
# 3) Write a function called change that takes any string, adds “Nice to meet you!” to the end of the argument given,
# and returns that new string.
# Answer:
def change(str):
return str + "Nice to meet you!"
print("Hello, " + change("whatever your name is, ")) # This line is not needed
# 4) Write a function, accum, that takes a list of integers as input and returns the sum of those integers.
# Answer:
def accum(lst):
lst_sum = 0
for numbs in lst:
lst_sum = lst_sum + numbs
return lst_sum
lst = [1,2,3,4,10,6,7,8,9] # can be any list of numbers
accum(lst)
print(accum(lst)) # print is not needed
# 5) Write a function, length, that takes in a list as the input. If the length of the list is greater than or equal to 5,
# return “Longer than 5”. If the length is less than 5, return “Less than 5”.
# Answer:
def length(lst):
if len(lst) >= 5:
return "Longer than 5"
return "Less than 5" # can also use 'else:' but not needed in this case
lst1 = [1,2,3]
lst2 = [1, 2, 3, 4, 5]
lst3 = [1, 2, 3, 4, 5, 6]
print(length(lst1)) # 'print' are not needed
print(length(lst2))
print(length(lst3))
# 6) You will need to write two functions for this problem. The first function,
# divide that takes in any number and returns that same number divided by 2.
# The second function called sum should take any number, divide it by 2, and add 6.
# It should return this new number. You should call the divide function within the sum function.
# Do not worry about decimals.
# Anwer:
def divide(numb):
return int(numb) // 2
def sum(numb):
return int(numb) // 2 + 6
# Next 3 lines are not needed for this excersise
number = input("Enter any number:") # User input not needed
print(divide(number)) # print not needed
print(sum(number))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment