-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpython_code.py
More file actions
124 lines (84 loc) · 1.83 KB
/
python_code.py
File metadata and controls
124 lines (84 loc) · 1.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# 1. Intro to Python
print("Hello, world!")
text = input("Type some text: ")
print("You typed", text)
# 2. Variables
age = 35
print(type(age))
money = 10.50
print(type(money))
isGameOver = False
print(type(isGameOver))
print(type(text))
# 3. Operators
# Assignment: =
# Arithmetic: + - * / % // **
# Comparison: == != >= > <= <
# Logical: and or not
# 4. Lists
inventory = ["Axe", "Food", "Helmet"]
food = inventory[1]
print(food)
inventory[1] = "Fruit"
print(inventory[0:2])
inventory.append("Water")
inventory.remove("Axe")
inventory.insert(1, "Knife")
print(inventory)
# 5. Tupels and Ranges
profile = ("Barry", 27)
name = profile[0]
print(type(profile))
my_range = range(5)
print(type(my_range))
# 6. Dictionaries
inventory = {"Axe": 1, "Fruit": 5, "Knife": 1}
print(type(inventory))
print(inventory.items())
num_axes = inventory["Axe"]
print(num_axes)
# 7. If Statements
pos = 5
key = "R"
if key == "R":
pos += 10
print("Player moved right")
elif key == "L":
pos -= 10
print("Player moved left")
else:
print("Unknown command")
# 8. While Loops
key = "Q"
while isGameOver:
print("The game is running...")
if key == "Q":
isGameOver = False
print("The game is over.")
# 9. For Loops
for count in range(4):
print(count + 1)
inventory = ["Axe", "Shield", "Boots"]
for item in inventory:
print(item)
# 10. Functions
def move(amount):
global pos
pos += amount
move(5)
print("Position", pos)
def move_from(pos, amount):
return pos + amount
print(move_from(0, 8))
# 11. Classes and Objects
class Hobgoblin():
def __init__(self):
self.name = "Hobgoblin"
self.health = 0
self.attack = 2
def bite(self):
print("The Hobgoblin bites!")
def run_away(self):
print("The Hobgoblin runs away.")
character = Hobgoblin()
print(type(character))