【TryHackMe】Python Basics：Walkthrough

# 概要

TryHackMe「Python Basics」のWalkthroughです。

https://tryhackme.com/r/room/pythonbasics

# Task2

## **Q1**.*On the code editor, print "Hello World". What is the flag?*

**Hint**.*In the code editor (right-hand side), replace "Learn security with TryHackMe" with "Hello World" and click the green run code button.*

`print()`で`Hello World`を出力します。

```python
print("Hello World")
```

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/3005822/7b4c9dd0-4ffc-ad24-2dce-4b4fb7f2d740.png)

**A**.**THM{PRINT_STATEMENTS}**

# Task3

## **Q1**.*In the code editor, print the result of 21 + 43. What is the flag?*

**Hint**.*In the code editor on the right, write the following: print(21 + 43)*

```python
print(21 + 43)
```

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/3005822/f434fc37-7f4d-1aa0-0b4d-eed042d3607d.png)

**A**.**THM{ADDITI0N}**

## **Q2**.*Print the result of 142 - 52. What is the flag?*

```python
print(142 - 52)
```

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/3005822/ea9ad2c3-9f36-ad8b-015f-e64e47c6dc78.png)

**A**.**THM{SUBTRCT}**

## **Q3**.*Print the result of 10 * 342. What is the flag?*

```python
print(10 * 342)
```

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/3005822/be901b05-31ef-790b-036a-482feda78883.png)

**A**.**THM{MULTIPLICATION_PYTHON}**

## **Q4**.*Print the result of 5 squared. What is the flag?*

**Hint**.*5 ** 2*

```python
print(5 ** 2)
```

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/3005822/8d0f08d9-0348-dc2b-314e-052e55289864.png)

**A**.**THM{EXP0N3NT_POWER}**

# Task4

## **Q3**.*On another new line, print out the value of height. What is the flag that appears?*

```python
height = 200
height += 50
print(height)
```

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/3005822/fb426260-3e8c-3acd-b9a9-c381cc3fb597.png)

**A**.**THM{VARIABL3S}**

# Task6

## **Q2**.*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**.*Need a hint? On the code editor, to the left of the "Run Code" button, there will be a hint button to help you get started.*

```python
shipping_cost_per_kg = 1.20
customer_basket_cost = 34
customer_basket_weight = 44

if(customer_basket_cost >= 100):
  print('Free shipping!')
else:
  shipping_cost = customer_basket_weight * shipping_cost_per_kg
  customer_basket_cost = shipping_cost + customer_basket_cost

print("Total basket cost including shipping is " + str(customer_basket_cost))
```

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/3005822/4fc3c2bb-fc3b-f53a-d143-edb1c0c8df9a.png)

**A**.**THM{IF_STATEMENT_SHOPPING}**

## **Q3**.*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.*

`customer_basket_cost`を`101`に変更します。

```python
shipping_cost_per_kg = 1.20
customer_basket_cost = 101
customer_basket_weight = 44

if(customer_basket_cost >= 100):
  print('Free shipping!')
else:
  shipping_cost = customer_basket_weight * shipping_cost_per_kg
  customer_basket_cost = shipping_cost + customer_basket_cost

print("Total basket cost including shipping is " + str(customer_basket_cost))
```

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/3005822/d0c8a712-dedb-1336-60bf-b8467e0cc5cf.png)

**A**.**THM{MY_FIRST_APP}**

# Task7

## **Q1**.*On the code editor, click back on the "script.py" tab and code a loop that outputs every number from 0 to 50.*

**Hint**.*You can use a for loop or while loop, but it must print from 0 to 50.*

```python
for i in range(51):
  print(i)
```

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/3005822/12585077-004e-ba03-bdd0-8b8c4c2bddb8.png)

**A**.**THM{L00PS_WHILE_FOR}**

# Task8

## **Q1**.*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).*

**Hint**.*Make sure your Python function has the name bitcoinToUSD and uses the parameter names bitcoin_amount and bitcoin_value_usd.*

```python
investment_in_bitcoin = 1.2
bitcoin_to_usd = 40000

# 1) write a function to calculate bitcoin to usd
def bitcoinToUSD(bitcoin_amount, bitcoin_value_usd):
  usd_value = bitcoin_amount * bitcoin_value_usd
  return usd_value

investment_in_usd = bitcoinToUSD(investment_in_bitcoin, bitcoin_to_usd)
if investment_in_usd <= 30000:
  print("Investment below $30,000! SELL!")
else:
  print("Investment above $30,000")
```

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/3005822/f18ddc5d-d5a0-db07-f6bc-ee7633bbc0cf.png)

**A**.**THM{BITC0IN_INVESTOR}**

# Task9

## **Q1**.*In the code editor, write Python code to read the flag.txt file. What is the flag in this file?*

```python
f = open("flag.txt", "r")
print(f.read())
```

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/3005822/1edd11c7-4293-0c93-d1b7-a69dfd0484de.png)

**A**.**THM{F1LE_R3AD}**