Skip to content

Commit d7d1e0c

Browse files
updated
1 parent c4b41f7 commit d7d1e0c

11 files changed

Lines changed: 244 additions & 5 deletions

File tree

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
---
2+
alwaysApply: true
3+
always_on: true
4+
trigger: always_on
5+
applyTo: "**"
6+
description: Snyk Security At Inception
7+
---
8+
9+
# Project security best practices
10+
11+
- Always run snyk_code_scan tool for new first party code that is generated in a Snyk-supported language.
12+
- If any security issues are found based on newly introduced or modified code or dependencies, attempt to fix the issues using the results context from Snyk.
13+
- Rescan the code after fixing the issues to ensure that the issues were fixed and that there are no newly introduced issues.
14+
- Repeat this process until no new issues are found.

40-scope-resolution/image.png

200 KB
Loading

40-scope-resolution/main.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,15 @@
11
# Scope Resolution
2+
# variable scope = where a variable is visible/accessible
3+
# scop resolution = the order in which Python looks for a variable
4+
# (LEGB Rule) Local -> Enclosing -> Global -> Built-in
5+
6+
def func1():
7+
x = 10 # Local scope
8+
print("Inside func1, x =", x)
9+
10+
def func2():
11+
x = 20 # Local scope
12+
print("Inside func2, x =", x) # Will look for x in enclosing/global scope
13+
14+
func1()
15+
func2()
1.52 KB
Binary file not shown.

41-if-name-main/app.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import calculator
2+
# from calculator import *
3+
4+
# Direct function calls
5+
print(calculator.add(10, 5)) # ➝ 15
6+
print(calculator.subtract(10, 5)) # ➝ 5
7+
print(calculator.multiply(10, 5)) # ➝ 50
8+
print(calculator.divide(10, 5)) # ➝ 2.0
9+
10+
# Using the dispatcher
11+
result = calculator.calculate('*', 7, 3)
12+
print(result) # ➝ 21

41-if-name-main/calculator.py

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
# If __name__ == '__main__': (this script can be imported OR run standalone)
2+
# Functions and classes in this modeule can be reused
3+
# when imported without executing the main block.
4+
# This is useful for creating reusable modules.
5+
6+
# ex. library = Import library for functionality
7+
# When running library directly, display a help page
8+
9+
"""
10+
calculator.py - A simple arithmetic library
11+
12+
Provides functions for basic math operations:
13+
- add
14+
- subtract
15+
- multiply
16+
- divide
17+
- calculate (dispatches based on operator)
18+
19+
Can be imported as a library OR run standalone.
20+
"""
21+
22+
def add(a, b):
23+
return a + b
24+
25+
def subtract(a, b):
26+
return a - b
27+
28+
def multiply(a, b):
29+
return a * b
30+
31+
def divide(a, b):
32+
if b == 0:
33+
raise ValueError("Division by zero is not allowed")
34+
return a / b
35+
36+
def calculate(operator, a, b):
37+
match operator:
38+
case '+':
39+
return add(a, b)
40+
case '-':
41+
return subtract(a, b)
42+
case '*':
43+
return multiply(a, b)
44+
case '/':
45+
return divide(a, b)
46+
case _:
47+
raise ValueError(f"Invalid operator: {operator}")
48+
49+
def main():
50+
try:
51+
operator = input("Enter operation (+, -, *, /): ").strip()
52+
a = float(input("Input value for a: "))
53+
b = float(input("Input value for b: "))
54+
result = calculate(operator, a, b)
55+
print(f"{a} {operator} {b} = {result}")
56+
except ValueError as e:
57+
print(f"Error: {e}")
58+
except Exception as e:
59+
print(f"Unexpected error: {e}")
60+
61+
if __name__ == '__main__':
62+
main()

41-if-name-main/main.py

Lines changed: 0 additions & 1 deletion
This file was deleted.
169 Bytes
Binary file not shown.

42-banking-program/main.py

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,59 @@
1-
# Banking Program
1+
# Python Banking Program
2+
3+
balance = 0 # global balance
4+
5+
def show_balance():
6+
print(f"Your current balance is: {balance}")
7+
8+
def deposit():
9+
global balance
10+
try:
11+
amount_to_deposit = float(input("Enter amount to deposit: "))
12+
if amount_to_deposit <= 0:
13+
print("Entered amount is not valid. Enter a positive amount.")
14+
return
15+
balance += amount_to_deposit
16+
print(f"Deposit successful! Your balance is now: {balance}")
17+
except ValueError:
18+
print("Invalid input. Please enter a numeric value.")
19+
20+
def withdraw():
21+
global balance
22+
try:
23+
amount_to_withdraw = float(input("Enter amount to withdraw: "))
24+
if amount_to_withdraw <= 0:
25+
print("Entered amount is not valid. Enter a positive amount.")
26+
return
27+
if amount_to_withdraw > balance:
28+
print("You don't have sufficient funds to make this withdrawal.")
29+
return
30+
balance -= amount_to_withdraw
31+
print(f"Withdrawal successful! Your balance is now: {balance}")
32+
except ValueError:
33+
print("Invalid input. Please enter a numeric value.")
34+
35+
def main():
36+
is_running = True
37+
while is_running:
38+
print("\nWelcome to XYZ Bank")
39+
print("1. Show Balance")
40+
print("2. Deposit")
41+
print("3. Withdraw")
42+
print("4. Exit")
43+
44+
choice = input("Enter your choice (1 - 4): ").strip()
45+
match choice:
46+
case "1":
47+
show_balance()
48+
case "2":
49+
deposit()
50+
case "3":
51+
withdraw()
52+
case "4":
53+
print("Thank you for banking with us!")
54+
is_running = False
55+
case _:
56+
print("Invalid choice. Please enter 1 - 4.")
57+
58+
if __name__ == "__main__":
59+
main()

43-slot-machine/main.py

Lines changed: 81 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,81 @@
1-
# Slot Machine
1+
# Python Slot Machine
2+
# user has balance
3+
# user can bet amount
4+
5+
import random
6+
7+
# Spin the slot machine (pick 3 random symbols)
8+
def spin_row():
9+
# symbols are cherry, lemon, orange, watermelon, star, bell, seven
10+
symbols = ['🍒', '🍋', '🍊', '🍉', '⭐', '🔔', '7️⃣']
11+
# choose 3 symbols randomly and return as a list
12+
return [random.choice(symbols) for _ in range(3)]
13+
14+
# Decide how much the player wins
15+
def calculate_payout(row, bet):
16+
# All three match
17+
if row[0] == row[1] == row[2]:
18+
# Jackpot if all sevens
19+
if row[0] == '7️⃣':
20+
return bet * 10 # Jackpot # award 10 times the bet
21+
else:
22+
return bet * 5 # Three of a kind # award 5 times the bet
23+
# Any two match
24+
elif row[0] == row[1] or row[1] == row[2] or row[0] == row[2]:
25+
return bet * 2 # Two of a kind # award 2 times the bet
26+
# No match # no winnings
27+
return 0
28+
29+
def main():
30+
# Initial balance for the player
31+
balance = 100
32+
print("🎰 Welcome to the Python Slot Machine!")
33+
print(f"Starting balance: ${balance}")
34+
35+
# Game loop - continues until player quits or runs out of money
36+
while balance > 0:
37+
print(f"\nYour balance: ${balance}")
38+
bet = input("Enter bet amount (0 to quit): ")
39+
40+
# Check if input is a number
41+
if not bet.isdigit():
42+
print("Please enter a number.")
43+
continue
44+
45+
bet = int(bet)
46+
47+
# Quit if bet is 0
48+
if bet == 0:
49+
print("Thanks for playing! Goodbye.")
50+
break
51+
52+
# Check if bet is valid
53+
# Bet cannot be more than balance or negative
54+
if bet > balance or bet < 0:
55+
print("Invalid bet amount.")
56+
continue
57+
58+
# Deduct bet from balance
59+
balance -= bet
60+
61+
# Spin the machine and display result
62+
row = spin_row()
63+
print("Spinning...")
64+
# Display the row
65+
print(" | ".join(row))
66+
67+
# Calculate winnings
68+
payout = calculate_payout(row, bet)
69+
# Update balance and display result
70+
if payout > 0:
71+
balance += payout
72+
print(f"🎉 You won ${payout}!")
73+
else:
74+
print("No win this time.")
75+
76+
# Game over message
77+
print("\nGame over! Final balance:", balance)
78+
79+
if __name__ == "__main__":
80+
main()
81+

0 commit comments

Comments
 (0)