Created
February 2, 2020 17:13
-
-
Save Cython-dot/01927d89ab91a78f694c3d816191ee8d to your computer and use it in GitHub Desktop.
Python 3 | Control Flow
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
""" | |
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: | |
return False | |
print(same_name("Colby", "Colby")) | |
# print: True | |
print(same_name("Tina", "Amber")) | |
# print: False | |
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
""" | |
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: | |
return False | |
print(in_range(10, 10, 10)) | |
# print: True | |
print(in_range(5, 10, 20)) | |
# print: False |
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
""" | |
Create a function named not_sum_to_ten() that has two parameters named num1 and num2. | |
Return True if num1 and num2 do not sum to 10. Return False otherwise. | |
""" | |
# Write your not_sum_to_ten function here: | |
def not_sum_to_ten(num1, num2): | |
if num1 + num2 != 10: | |
return True | |
else: | |
return False | |
print(not_sum_to_ten(9, -1)) | |
# print: True | |
print(not_sum_to_ten(9, 1)) | |
# print: False | |
print(not_sum_to_ten(5,5)) | |
# print: False | |
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
""" | |
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): | |
if num >= 0 and num <=0: | |
return False | |
else: | |
return False | |
print(always_false(0)) | |
# print: False | |
print(always_false(-1)) | |
# print: False | |
print(always_false(1)) | |
# print: False |
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
""" | |
Create a function called over_budget that has five parameters named budget, food_bill, electricity_bill, internet_bill, and rent. | |
The function should return True if budget is less than the sum of the other four parameters. | |
You’ve gone over budget! Return False otherwise. | |
""" | |
# Write your over_budget function here: | |
def over_budget(budget, food_bill, electricity_bill, internet_bill, rent): | |
if budget < (food_bill + electricity_bill + internet_bill + rent): | |
return True | |
else: | |
return False | |
print(over_budget(100, 20, 30, 10, 40)) | |
# print: False | |
print(over_budget(80, 20, 30, 10, 30)) | |
# print: True |
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
""" | |
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 | |
elif num2 > num1 and num2 > num3: | |
return num2 | |
elif num3 > num1 and num3 > num2: | |
return num3 | |
else: | |
return "It's a tie!" | |
print(max_num(-10, 0, 10)) | |
# print: 10 | |
print(max_num(-10, 5, -30)) | |
# print: 5 | |
print(max_num(-5, -10, -10)) | |
# print: -5 | |
print(max_num(5, 10, 0)) | |
# print: 10 | |
print(max_num(2, 3, 3)) | |
# print: "It's a tie!" | |
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
""" | |
Create a function called divisible_by_ten() that has one parameter named num. | |
The function should return True if num is divisible by 10, and False otherwise. | |
Consider how to use modulo (%) to check for divisibility. | |
""" | |
# Write your divisible_by_ten function here: | |
def divisible_by_ten(num): | |
if ((num % 10 == 0)): | |
return True | |
else: | |
return False | |
print(divisible_by_ten(20)) | |
# print: True | |
print(divisible_by_ten(25)) | |
# print: False | |
print(divisible_by_ten(50)) | |
# print: True |
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
""" | |
Create a function named large_power() that takes two parameters named base and exponent. | |
If base raised to the exponent is greater than 5000, return True, otherwise return False | |
""" | |
# Write your large_power function here: | |
def large_power(base, exponent): | |
if base**exponent > 5000: | |
return True | |
else: | |
return False | |
print(large_power(2, 13)) | |
# print: True | |
print(large_power(2, 12)) | |
# print: False |
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
""" | |
Create a function named twice_as_large() that has two parameters named num1 and num2. | |
Return True if num1 is more than double num2. Return False otherwise. | |
""" | |
# Write your twice_as_large function here: | |
def twice_as_large(num1, num2): | |
if num1 > num2 * 2: | |
return True | |
else: | |
return False | |
print(twice_as_large(10, 5)) | |
# print: False | |
print(twice_as_large(11, 5)) | |
# print: True | |
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
""" | |
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!" | |
elif rating > 5 and rating < 9: | |
return "This one was fun." | |
else: | |
return "Outstanding!" | |
print(movie_review(9)) | |
# print: "Outstanding!" | |
print(movie_review(4)) | |
# print: "Avoid at all costs!" | |
print(movie_review(6)) | |
# print: "This one was fun." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment