TryHackMe - Python Basics Writeup

TryHackMe logo for Python Basic room

This room aims to teach us about using a web-based code editor, learn the basics of Python and put our knowledge into practice by eventually coding a short Bitcoin investment project.

This TryHackMe room can be accessed using this link: https://tryhackme.com/room/pythonbasics.

Important: Please use the CODE HINT to either get the code or get the hint that can almost solve your issue. Also, take proper care of indentation while running the codes.

Task 2: Hello World

On the code editor, print “Hello World”. What is the flag? Code Hint: print(“Hello World”)

Task 3: Mathematical Operators

Arithmetic Operators
Comparison Operators
  1. In the code editor, print the result of 21 + 43. What is the flag? Code Hint : print(21 + 43)
  2. Print the result of 142–52. What is the flag? Code Hint: print ( 142- 52)
  3. Print the result of 10 * 342. What is the flag? Code Hint: print ( 10 * 342)
  4. Print the result of 5 squared. What is the flag? Code Hint: print ( 5 ** 2)

Task 4: Variables and Data Types

Variables allow us to store and update data in a computer program. We have a variable name and we can store the data to that name.

Data Types, which is the type of data being stored in a variable. We can store text, numbers, and many other types. The data types to know are:

  • String- Used for combinations of characters, such as letters or symbols.
  • Integer- Whole numbers.
  • Float- Numbers that contain decimal points, or for Fractions.
  • Boolean- Used for data that is restricted to True or False options.
  • List- Series of different data types stored in a collection.

Q&A

  1. In the code editor, create a variable called height and set its initial value to 200. Code Hint: height = 200 (No answer needed).
  2. On a new line, add 50 to the height variable. Code Hint: height += 50 ( No answer needed)
  3. On another new line, print out the value of height. What is the flag that appears? Code Hint: print ( height)

Task 5: Logical and Boolean Operators

Logical operators allow assignment and comparisons to be made and are used in conditional testing (such as if statements).

Logical Operators

Boolean operators are used to connect and compare relationships between statements. Like an, if statement, conditions can be true or false.

Boolean Operators

Task 6: Shipping Project Introduction to If Statements

Using “if statements” allows programs to make decisions. They let a program choose a decision based on a condition.

There are some key components we note from our code example above:

  • The if the keyword indicates the beginning of the if statement, followed by a set of conditions.
  • The if statement is only run if the condition (or sets of conditions) is true. In our example, it’s age < 17; if that condition is true (age is above 17), the code within the if statement runs. Per the example, if certain conditions are not met, the program can default to running code shown in the else part of the if statement.
  • A colon : marks the end of the if statement.
  • Note the indentation. Anything after the colon that is indented, is considered part of the if statement, which the program will execute.

This code is to be executed in the ‘shipping.py’ tab.

Q&A

  1. In this exercise, we will code a small application that calculates and outputs the shipping cost for a customer based on how much they’ve spent. In the code editor, click on the “shipping.py” tab and follow the instructions to complete this task. Code Hint: if ( customer_basket_cost > 100):
    print ( customer_basket_cost)
    else:
    print (customer_basket_cost + customer_basket_weight * 1.2)
  2. Once you’ve written the application in the code editor’s shipping.py tab, a flag will appear, which is the answer to this question. Hint: THM{IF_STATEMENT*********}
  3. In shipping.py, on line 12 (when using the Code Editor’s Hint), change the customer_basket_cost variable to 101 and re-run your code. You will get a flag (if the total cost is correct based on your code); the flag is the answer to this question. Code Hint: Change the ‘customer_basket_cost’ value from 34 to 101

Task 7: Loops

In programming, loops allow programs to iterate and perform actions a number of times. There are two types of loops, for and while loops.

While Loop:

We can have this loop run indefinitely or (similar to an if statement) determine how many times the loop should run based on a condition.

i = 1
while i <= 10:
print(i)
i = i + 1

For Loop:

A for loop is used to iterate over a sequence such as a list. Lists are used to store multiple items in a single variable and are created using square brackets.

for i in range(5):
print(i)

Q&A

  1. On the code editor, click back on the “script.py” tab and code a loop that outputs every number from 0 to 50. Code Hint: i = 0
    while i <=50 :
    print ( i)
    i += 1

Task 8: Bitcoin Project Introduction to Functions

A function is a block of code that can be called at different places in your program. Having functions removes repetitive code, as the function’s purpose can be used multiple times throughout a program.

There are some key components we can note from this function:

  • The def keyword indicates the beginning of a function. The function is followed by a name that the programmer defines (and is a function parameter). In our example, it's sayHello.
  • Following the function name is a pair of parenthesis () that holds input values, data that we can pass into the function. In our example, it's a name.
  • A colon : marks the end of the function header.

In the function, notice the indentation. Similar to if statements, anything after the colons that is indented is considered part of the function.

This code is to be run in the ‘bitcoin.py’ tab.

Q&A

  1. Once you’ve written the bitcoinToUSD function, use it to calculate the value of your Bitcoin in USD, and then create an if statement to determine if the value falls below $30,000; if it does, output a message to alert you (via a print statement). Code Hint: for using Function, ‘if ( bitcoinToUSD( investment_in_bitcoin , bitcoin_to_usd) < 30000):
    print(“Alert”)
    else:
    print( bitcoinToUSD( investment_in_bitcoin , bitcoin_to_usd) )’ & for writing Function, ‘def bitcoinToUSD(bitcoin_amount, bitcoin_value_usd):
    usd_value = bitcoin_amount * bitcoin_value_usd
    return usd_value’
  2. 1 Bitcoin is now worth $25,000. In the code editor on line 14, update the bitcoin_to_usd variable value to 25000 and see if your Python program recognises that your investment is below the $30,000 threshold. Code Hint: Change the value of bitcoin_to_usd from 40000 to 25000

Task 9: Files

In Python, we can read and write from files. We’ve seen that in cyber security, it’s common to write a script and import or export it from a file; whether that be as a way to store the output of your script or to import a list of hundreds of websites from a file to enumerate. Let’s dive straight into an example:

f = open("file_name", "r")
print(f.read())

To open the file, we use the built-in open() function, and the “r” parameter stands for “read” and is used as we’re reading the contents of the file. The variable has a read() method for reading the contents of the file. You can also use the readlines() method and loop over each line in the file; useful if you have a list where each item is on a new line. In the example above, the file is in the same folder as the Python script; if it were elsewhere, you would need to specify the full path of the file.

Q&A

  1. In the code editor, write Python code to read the flag.txt file. What is the flag in this file? Code Hint: file = open(“flag.txt”, “r”)
    print(file.read())

Task 10: Imports

In Python, we can import libraries, which are a collection of files that contain functions. We import the libraries using the import keyword. Then in Python, we use that import's library name to reference its functions. In the example above, we import datetime, then access the .now() method by calling library_name.method_name(). Copy and paste the example above into the code editor.

Important: Take proper care of indentation while copying the code. If you see any kind of error while running the above codes, please debug the codes for proper indentation.

Hope this write-up helped you.

I would love to get connected with you on LinkedIn: https://www.linkedin.com/in/-prashantkumar07/

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store