Topic_1_Homeworks
Topic_1_Homeworks
Topic 1: Homework
Dr. Marcus Roel
Spring 2024, UNNC
Outline
Problem Set 1
Text-Based Coding
if/else
input()
str.lower()
Problem Sets are really an invitation to playing around with code (and fail many
times).
feel free to write more than one solution (if question permits)
Don't worry if most of what you write is either trivial or badly written (for now)
Repeat Material
Standard-homework: Go over our examples from the lecture and (1) understand
them, and (2) vary them slightly to see if you really understand them.
I can't stress enough how important this is
Text-Based Coding
Write your own morning routine, evening routine, or any other task sequence that
you do.
You can do this for literally everything you do. Feel free to be creative : )
MR: I hope you enjoyed this : ).
Functions
Write a function that (a) adds two numbers, (b) multiplies 3 numbers with each
other, and (c) computers some other mathematical problem (e.g., the length of the
hypotenuse in a right triangle?) of your liking.
In [ ]: def add_two_numbers(x, y):
return x + y
Write a function takes subtracts the second argument from the first if both
arguments are positive. It returns 0 if one or both arguments are negative.
In [20]: def subtract_two_positive_numbers(x, y):
if x >= 0 and y >= 0:
return x - y
else:
return 0
4-1 yields 3
4-(-10) yields 0
print()
use the print() function to write some fun things to your screen.
if you combine more than one string variable, pay particular attention to spaces
between words
Use "\n" to force a newline (and again, pay attention to spacing).
See other questions below.
Turn the following if-check into a function that compares two inputs.
In [23]: x = 5
y = 6
if x > y:
print(x, 'is bigger than', y)
else:
print(x, 'is not bigger than', y)
5 is not bigger than 6
# Test:
is_bigger_than(5, 6)
input()
Write a function that uses input() and a if/else comparison.
In [25]: def welcome():
myself = "Marcus"
if person == myself:
print(f"Oh! Hi {person}, I hope you're having a good day!")
elif person == "Elsa":
print(f"Ohhhh, so, have you managed to let-it-go yet?")
else:
print(f"It is very nice to meet you, {person}! How are you?")
welcome()
Question: given your insight to the prior question, in plain language, how could you
adjust the code?
Solution
In [30]: text_capitalized = "Hello World"
print(f"Applying .lower() to '{text_capitalized}' yields:")
print(f"{text_capitalized.lower()}")
# Done in 2 print commands due to slides formatting