Problem Solving with Programming


Part 2 of a multi-part series course. Visit the Connect Learning Academy to take the entire course.



Programming is all about solving a problem using a set of words that communicate with a computer to produce what you want. Being able to change your code and solve problems on the spot is one of the most key aspects of producing a decent product. You can’t always look to the internet for your solution, so you need to prepare.


Where were we, now?


On our first trip across beginner programming in Python, we wrote a coin flipping program. With that we already had set foot into a few different concepts.

  • How to structure a program
  • Using a tuple
  • Importing and applying modules to your code
  • For loops and while loops
  • Writing a program that expects the user to make an error

With this knowledge, you’re already several steps ahead of anyone else that’s just starting. But how can you ever know where to apply this without context? Or, even, without knowing what else you can do with it? These are a few important questions you need to ask and think about in regards to anything you’re working on. Logic and memorization are key aspects to getting the job done, and you’ll need to have them on mind all the time.


Dress it up, why don't you? Let’s move on.


With any role in society, like being a graphic designer or a police officer you have knowledge gained from experience or education that you’re expected to use on a day-to-day basis. This has become like clockwork to you, a cycle of thought that you understand and use whenever it’s needed. And as it should be, it’s the same with programming. This is equally important with learning. Consider this; your knowledge is the light in an abyss of darkness. Every time you shine more light in the darkness, the darkness becomes bigger and bigger. You can use what you’ve learned to learn more, but you’ll never know it all. And that can be a beautiful part, not knowing, because you understand that there’s still a possibility to know, and the only thing that’s holding you from your goal is the work it will take to get there.

So, let’s look at our code from before and see what we can do with it then.

Well, let’s say we want to see how many of heads or tails we get and show the number. Alright, let’s modify that coinFlip function.

# Instead of print (random.choice(coinSides))
# Let's make it into a variable
def coinFlip(userNumber):
for i in range(userNumber):
x = random.choice(coinSides)

Alright, so we’ve assigned the variable x to the value random.choice(coinSides). That means that every time this loops, x is the new value given. x can now be HEADS or TAILS. But now that we’ve done this, it doesn’t show us what our value is. Now we have a problem in the way of our solution, but let’s finish the first part of this.

# Okay, we have a variable. Now.. let's use some if statements.
# And let's make two new variables, tailResult and headResult
tailResult = 0
headResult = 0
def coinFlip(userNumber):
for i in range(userNumber):
x = random.choice(coinSides)
if x == 'TAILS': # This checks if x is equal to tails
tailResult += 1 # If x equals tails, then add 1
# to our variable tailResult
elif x == 'HEADS': # If x doesn't equal tails, but
# does equal heads
headResult += 1 # Adds 1 to headResult instead.

Okay. Now we have a variable, x, that is considered the result of what random.choice(coinSides) produces. In one cycle of the loop, if x is equal to ‘TAILS’ then we add 1 to the other variable, tailResult. If x is instead equal to ‘HEADS’, then we will add 1 to headResult.

Before we move on, let’s look at our newest introduced operator here.

# The += operator, instead of assigning or appending a value
# just adds a value to the total. Because we're using numbers,
# this will add to a total number.
# example:
x = 2
x += 3
# The result would be that x equaled 5. It doesn't show you,
# but now the code says that X equals 5 and only 5. Not 2 anymore.
# The = part of our complete += operator is a sign for the
# programmer to see that the code assigns the new total value.
# To represent this mathematically:
x = x + 2

Alright. So we’ve come through, solving our first part. But now it doesn’t print our result and we have even more variables sitting around.

So let’s add a print function.

Now it prints out x, the headResult, and our tailsResult all together.

Let’s just put the original code into a function, main(), and make it all run together nicely.

Here’s what we have at the end.

I added the again() function so that the user can run the program again. It’s fairly clear in the code what it does.

The result is a fast, efficient way to flip a coin and have some nice functionality at the same time.


Code on.



Disclaimer:
The article (collectively; 'This', 'the article', 'this part', 'part one') is not meant to represent the results of professional programming or working in a professional environment. It is meant to provide the basis of learning to the reader. We are not responsible for anything you do or produce after reading.