Skip to content

Instantly share code, notes, and snippets.

"""
Create a function named in_range() that has three parameters named num, lower, and upper.
The function should return True if num is greater than or equal to lower and less than or equal to upper. Otherwise, return False.
"""
# Write your in_range function here:
def in_range(num, lower, upper):
if (num >= lower and num <= upper):
return True
else:
"""
Create a function named same_name() that has two parameters named your_name and my_name.
If our names are identical, return True. Otherwise, return False.
"""
# Write your same_name function here:
def same_name(your_name, my_name):
if (your_name == my_name):
return True
else:
"""
Create a function named always_false() that has one parameter named num.
Using an if statement, your variable num, and the operators >, and <, make it so your function will return False no matter what number is stored in num.
An if statement that is always false is called a contradiction. You will rarely want to do this while programming, but it is important to realize it is possible to do this.
"""
# Write your always_false function here:
def always_false(num):
"""
Create a function named movie_review() that has one parameter named rating.
If rating is less than or equal to 5, return "Avoid at all costs!". If rating is between 5 and 9, return "This one was fun.". If rating is 9 or above, return "Outstanding!"
"""
# Write your movie_review function here:
def movie_review(rating):
if (rating <= 5):
return "Avoid at all costs!"
if (rating < 9 and rating > 5):
"""
Create a function called max_num() that has three parameters named num1, num2, and num3.
The function should return the largest of these three numbers. If any of two numbers tie as the largest, you should return "It's a tie!".
"""
# Write your max_num function here:
def max_num (num1, num2, num3):
if (num1 > num2 and num1 > num3):
return num1
"""
Write a program that asks the user for the weight of their package and then tells them which method of shipping is cheapest and how much it will cost to ship their package using Sal’s Shippers.
"""
def ground_shipping(weight):
flat = 20
# can also do if, elif instead of all if statements
if (weight <= 2):
return (1.5 * weight) + flat
if (weight > 2 and weight <= 6):
return (3.0 * weight) + flat
"""
Write a function named append_sum that has one parameter — a list named named lst.
The function should add the last two elements of lst together and append the result to lst. It should do this process three times and then return lst.
For example, if lst started as [1, 1, 2], the final result should be [1, 1, 2, 3, 5, 8].
"""
#Write your function here
def append_sum(lst):
"""
Write a function named larger_list that has two parameters named lst1 and lst2.
The function should return the last element of the list that contains more elements. If both lists are the same size, then return the last element of lst1.
"""
#Write your function here
def larger_list(lst1, lst2):
if(len(lst1) >= len(lst2)):
return lst1[-1]
"""
Create a function named more_than_n that has three parameters named lst, item, and n.
The function should return True if item appears in the list more than n times. The function should return False otherwise.
"""
#Write your function here
def more_than_n(lst, item, n):
if (lst.count(item) > n):
return True
"""
Create a function called append_size that has one parameter named lst.
The function should append the size of lst (inclusive) to the end of lst. The function should then return this new list.
For example, if lst was [23, 42, 108], the function should return [23, 42, 108, 3] because the size of lst was originally 3.
"""
#Write your function here
def append_size(lst):
lst.append(len(lst))