Skip to content

Instantly share code, notes, and snippets.

View lunadora's full-sized avatar

lunadora

View GitHub Profile
@lunadora
lunadora / functions4.py
Created August 16, 2022 13:17
Question 4 Let's revisit our lucky_number function. We want to change it, so that instead of printing the message, it returns the message. This way, the calling line can print the message, or do something else with it if needed. Fill in the blanks to complete the code to make it work.
def lucky_number(name):
number = len(name) * 9
message = "Hello " + name + ". Your lucky number is " + str(number)
return message
print(lucky_number("Kay"))
print(lucky_number("Cameron"))
@lunadora
lunadora / functions2.py
Created August 16, 2022 13:14
Question 2 This function compares two numbers and returns them in increasing order. Fill in the blanks, so the print statement displays the result of the function call in order. Hint: if a function returns multiple values, don't forget to store these values in multiple variables
# This function compares two numbers and returns them
# in increasing order.
def order_numbers(number1, number2):
if number2 > number1:
return number1, number2
else:
return number2, number1
# 1) Fill in the blanks so the print statement displays the result
# of the function call
@lunadora
lunadora / functions1.py
Created August 16, 2022 13:09
Question 1 This function converts miles to kilometers (km). Complete the function to return the result of the conversion Call the function to convert the trip distance from miles to kilometers Fill in the blank to print the result of the conversion Calculate the round-trip in kilometers by doubling the result, and fill in the blank to print the …
# 1) Complete the function to return the result of the conversion
def convert_distance(miles):
km = miles * 1.6 # approximately 1.6 km in 1 mile
return(km)
my_trip_miles = 55
# 2) Convert my_trip_miles to kilometers by calling the function above
my_trip_km = convert_distance(my_trip_miles)
@lunadora
lunadora / codestyle.py
Created August 16, 2022 13:00
This function to calculate the area of a rectangle is not very readable. Can you refactor it, and then call the function to calculate the area with base of 5 and height of 6? Tip: a function that calculates the area of a rectangle should probably be called rectangle_area, and if it's receiving base and height, that's what the parameters should b…
def rectangle_area(base, height):
area = base*height # the area is base*height
print("The area is " + str(area))
rectangle_area(5,6)
@lunadora
lunadora / princodereuse.py
Created August 16, 2022 11:26
In this code, identify the repeated pattern and replace it with a function called month_days, that receives the name of the month and the number of days in that month as parameters. Adapt the rest of the code so that the result is the same. Confirm your results by making a function call with the correct parameters for both months listed.
# REPLACE THIS STARTER CODE WITH YOUR FUNCTION
#june_days = 30
#print("June has " + str(june_days) + " days.")
#july_days = 31
#print("July has " + str(july_days) + " days.")
def month_days(month,days):
print(month + " has " + str(days) + " days.")
month_days("June","30")
month_days("July","31")
@lunadora
lunadora / returningvalues.py
Created August 16, 2022 10:58
Use the get_seconds function to work out the amount of seconds in 2 hours and 30 minutes, then add this number to the amount of seconds in 45 minutes and 15 seconds. Then print the result.
def get_seconds(hours, minutes, seconds):
return 3600*hours + 60*minutes + seconds
amount_a = get_seconds(2,30,0)
amount_b = get_seconds(0,45,15)
result = amount_a + amount_b
print(result)
@lunadora
lunadora / functions.py
Created August 16, 2022 09:02
Flesh out the body of the print_seconds function so that it prints the total amount of seconds given the hours, minutes, and seconds function parameters. Remember that there are 3600 seconds in an hour and 60 seconds in a minute.
def print_seconds(hours, minutes, seconds):
print(3600*hours + 60*minutes + seconds)
print_seconds(1,2,3)
@lunadora
lunadora / ExpressionandVariables2.py
Last active August 16, 2022 08:54
Question 3 Combine the variables to display the sentence "How do you like Python so far?"
word1 = "How"
word2 = "do"
word3 = "you"
word4 = "like"
word5 = "Python"
word6 = "so"
word7 = "far?"
print(word1, word2, word3, word4, word5, word6, word7)
//print(' '.join([eval(str('word'+str(i+1))) for i in range(7)]))
@lunadora
lunadora / ExpressionandVariables1.py
Last active August 16, 2022 08:37
In this scenario, two friends are eating dinner at a restaurant. The bill comes in the amount of 47.28 dollars. The friends decide to split the bill evenly between them, after adding 15% tip for the service. Calculate the tip, the total amount to pay, and each friend's share, then output a message saying "Each person needs to pay: " followed by …
bill = 47.28
tip = (bill * (15/100))
total = bill + tip
share = total/2
print("Each person needs to pay: " + str(share))