0% found this document useful (0 votes)
8 views

Topic_1_Homeworks

The document outlines the homework for a Python programming course focused on economics, emphasizing the importance of experimentation and understanding coding concepts. It includes instructions for creating functions, using conditional statements, and implementing basic coding exercises such as a morning routine and a hangman game. The document encourages creativity and flexibility in coding, while also highlighting the significance of handling user inputs effectively.

Uploaded by

James SI
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Topic_1_Homeworks

The document outlines the homework for a Python programming course focused on economics, emphasizing the importance of experimentation and understanding coding concepts. It includes instructions for creating functions, using conditional statements, and implementing basic coding exercises such as a morning routine and a hangman game. The document encourages creativity and flexibility in coding, while also highlighting the significance of handling user inputs effectively.

Uploaded by

James SI
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Python Programming for Economics

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

In [1]: def multiply_three_numbers(x, y, z):


return x*y*z

In [3]: # (c) curious to hear what you did!

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

In [21]: # Let's test it


print("4-1 yields", subtract_two_positive_numbers(4, 1))
print("4-(-10) yields", subtract_two_positive_numbers(4, -10))

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

In [17]: def is_bigger_than(x, y):


if x > y:
print(x, 'is bigger than', y)
# Optional. Maybe it's a good idea to do more than just print
return True
else:
print(x, 'is not bigger than', y)
return False

# Test:
is_bigger_than(5, 6)

5 is not bigger than 10


Out[17]: False

input()
Write a function that uses input() and a if/else comparison.
In [25]: def welcome():
myself = "Marcus"

# Instead of using print("text") then input(),


# we can add a text into input directly:
person = input("Hi, what's your name?")

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()

It is very nice to meet you, Joel! How are you?

Question: what happens if you input "marcus" or "elsa"?


In [22]: welcome() # Check question. Before checking, what's your guess?

It is very nice to meet you, marcus! How are you?

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

Applying .lower() to 'Hello World' yields:


hello world

In [32]: text_capitalized = "Hello World"


print(f"Using str.lower(text_variable) works as well:")
print(f"{str.lower(text_capitalized)}")
# Done in 2 print commands due to slides formatting

Using str.lower(text_variable) works as well:


hello world

What? Why do the two commands do the same thing?


str.lower(): 'str' is the general datatype for text. It has many built in methods
(functions)
.method(text) essentially calls a method (which is a function written inside a
'class') with a text as an argument passed in
Since any text variable is based on the datatype string, it can call the very same
methods as well!
to do so, just use ".method()", which is then called on itself.
We will cover classes in more details later. For now, just think of them as objects that
have build in features, which make our life easier.
Over time, you'll learn many of these convenient methods (but honestly, you'll just
'google' them most of the time)
Exercise: rewrite welcome() in light of the discussion above.
General Takeaway
You want your code to be 'fairly' flexible to handle typical use-cases
more important the less control you have about inputs (other users,
collaborators, various datasources)
more important if small deviations break your algorithm, yield different results,
or even break your code
personally comment: when i code simple things or just prototype, this is
something i typically don't worry much about.
Hangman
Using simple language only (no coding yet) write one (or more) functions to describe the
popular children's game "hang-man", i.e., guess letters from a secret word (of a known
length). You must guess it fully before missing 7 times.
General Algorithm of Hangman
[1] A secret word is selected, and numbers of letters are indicated as follows:
"_ _ _ _ _ _"
[2] Guessing Starts, which has the following structure:
[3] If 7 misses: GameOver, otherwise
[3.1] guess a letter
[3.2]check if letter is part of secret word;
if true, write letter at the correct place of "_ _ _ _ _ _"
if false, increase misses by 1;
advanced features: track wrong letters
advanced features: draw next part of the gallows
go back to [3].
Coding the the Hangman Game
you will need loops (either while or for) and lists from Lecture 2
obviously, next week's homework!

You might also like