What I learned
In this part, there will be new fundamental concepts that you have probably heard of and that are very useful in your Python journey.
Strings And Integers
As you probably know, there are two main types of text: strings and integers. Strings are the text, characters and letters we type. Integers, on the other hand, are the numbers we write in math. You might think there isn't a big difference between them when it comes to Python, but think again.
In this first example, I will demonstrate the importance of dividing and grouping strings and integers.
num1 = input('Give me a number: ')
num2 = input('Give me another number: ')
total = num1 + num2
print(f'{num1} + {num2} is equal to {total}')
In this code, we have 2 inputs called num1 and num2. They both ask a question, and this question asks us to type in a number. Underneath the first 2 lines, we create a variable called total. total gets the two values of the inputs and adds them. Simple, right? Well, when we print out this output in the last line, as you can see, the output is not what you think.
It's weird, isn't it? Well, Python, in this case, thinks these "numbers" are strings, and like any ordinary string, Python thinks that it can just join them together, and that's the answer your thinking of. To correct this mistake, just add an int followed by brackets around the input.
num1 = int(input('Give me a number: '))
num2 = int(input('Give me another number: '))
total = num1 + num2
print(f'{num1} + {num2} is equal to {total}')
Now, when we run this code, it should show the correct answer.
Indeed, it does. Now you know the importance of differentiating integers and strings.
If Statements
If statements, in my opinion, are by far one of the most important components of any Python application or code. If you have no idea what an if statement is, essentially, in a nutshell, it is a piece of code that makes decisions. For example, you could have an if statement where it says, If it is sunny, then go to the beach; otherwise, stay home.
In Python code, it is as simple as that. All you have to do is write the word If, followed by your condition.
temp = int(input('How hot is your pie in degrees Celcius? '))
if temp < 5:
print('Your pie is too cold to eat.')
elif temp > 40:
print('Your pie is too hot to eat.')
else:
print("Your pie is perfect to eat.")
In this example, you can see that we ask for an input. We are now using the int input as it asks for a temperature. Then we use the if statement. If the temp is lower than 5, then we will print, it is too cold. But you can also see that we have 2 other functions. These functions are all part of the if statement, but they allow us to add more to the if statement. In this case, elif the temp is hotter than 40, it is too hot to eat. Then else, means that if temp is between 5 and 40, it will print, your pie is perfect.
Comparision Operators
Comparision operators are used to symbolise things like equal to and higher than or lower than in python. They are mostly used in if statements so they will help when using if statements.
Conditions With SubStrings
Conditions with substrings are a function that allows you to find certain text, within another text. For example:
clothing = 'A velour jumpsuit.'
if 'velour' in clothing:
print("That's cool.")
else:
print('Groovy.')
This code consists of a variable called clothing. Underneath, we have an if statement but instead of using comparison operators or integers, here we use a string. This is called a condition with a substring. Continuing, we reference clothing in the if statement to show that we want to find this string inside of the clothing variable. If it does find that 'velour' is inside of the variable, it will print out 'That's cool.', else, 'Groovy.'
More Conditions
Another cool condition that we can use is the isupper and islower condition. Let me show you what it does.
phrase = input('Give us a shout! ')
if phrase.isupper():
print('Not so loud!')
else:
print("I can't hear you!")
This program tells us whether you are shouting loud enough by seeing the capitalisation of the code. This is pretty self-explanatory, but the isupper or islower are just conditions we can use in if statements compared to the lower and upper statements that we use in general code. In the first line of code, we have an input, then there is an if statement on the next line that uses this variable input. But instead of using it indirectly, we attach the isupper condition to it. This condition checks if the user input is all uppercase; if it is, we will go to the first condition, which is 'Not so load', but if the input is not all capital, we will then be redirected to the second condition, which is 'I can't hear you!'
Big-Challenge
As the last blog has explained, there will either be a mini-challenge or a big-challenge. In this blog, there will be a big challenge that uses both blogs information of what you have learnt. Lets move on.
Prompt:
A local coffee cart has set up at a building construction site. Due to the construction noise, orders need to be shouted to be heard.
Write a program that will ask a customer which type of drink they would like, as well as how many drinks they want.
If the drink order is in uppercase, your program should say:
WHAT DRINK DO YOU WANT TO ORDER? COFFEE
HOW MANY? 4
4 COFFEES COMING RIGHT UP!
If the drink order is in lowercase your program should say:
WHAT DRINK DO YOU WANT TO ORDER? tea
I DIDN'T HEAR YOUR ORDER!
If the drink order is not in uppercase or lowercase, your program should say:
WHAT DRINK DO YOU WANT TO ORDER? HoT ChOcOlAtE
I CAN'T UNDERSTAND YOU!
Solution:
If you are having a hard time solving this or have already finished the problem, here is the solution.
Drink = input("WHAT DRINK DO YOU WANT TO ORDER? ")
if Drink.isupper():
Amount = int(input("HOW MANY? "))
if Amount > 2:
print(f'{Amount} {Drink}S COMING RIGHT UP!')
elif Amount < 2:
print(f'{Amount} {Drink} COMING RIGHT UP!')
elif Drink.islower():
print("I DIDN'T HEAR YOUR ORDER!")
else:
print("I CAN'T UNDERSTAND YOU!")
You might freak out at first, but trust me, this code is very simple. The first step is to create an input called Drink. Next, we will start with the if statements. The first if statement checks whether your answer is in uppercase. If it isn't then we see if is is in all lower, if it is we choose that option but if it isn't we choose the else option. But if the order is in all uppercase, then we ask for the amount they want. Moving to the next if statement, we look at how many they ordered, if it is more than 2, we print out the statement of what they ordered and how many, but since there is more than one, we put an s at the end of the amount. But if there is less than 2, we can just print out the same thing except without the s at the end of the amount.
Conclusion
In conclusion, this is the end of week1 of the coding competition, stay tuned to see and learn all 5 weeks of this coding competition and hope you enjoy it as much as I do. In summary, today in this blog, we have learnt about, strings and integers, if statements, comparison operators and conditions with substrings. In the next blog, you will learn about, functions, parameters and arguments, as well as function mistakes.