Share 7 python codes for every day use

## 7 mã Python để sử dụng hàng ngày

Python là một ngôn ngữ lập trình mạnh mẽ và linh hoạt, có thể được sử dụng cho nhiều nhiệm vụ khác nhau.Dưới đây là bảy mã Python mà bạn có thể sử dụng trong cuộc sống hàng ngày của mình:

1. ** Một máy tính đơn giản **.Mã này sẽ cho phép bạn thực hiện các hoạt động số học cơ bản, chẳng hạn như bổ sung, trừ, nhân và chia.

`` `Python
Def Tính toán (A, B, OP):
Nếu op == '+':
trả lại a + b
Elif op == '-':
Trả lại A - B
Elif op == '*':
trả lại a * b
Elif op == '/':
trả lại a / b
`` `

2. ** Danh sách việc cần làm **.Mã này sẽ cho phép bạn tạo một danh sách việc cần làm và theo dõi các tác vụ của bạn.

`` `Python
def created_todo_list ():
todo_list = []
Trong khi đúng:
Task = input ('Nhập một tác vụ:')
Nếu nhiệm vụ == '':
phá vỡ
todo_list.append (Nhiệm vụ)
Quay lại TODO_LIST

def print_todo_list (todo_list):
Đối với nhiệm vụ trong TODO_LIST:
In (Nhiệm vụ)

DEF Mark_TASK_AS_COMPLETE (TODO_LIST, TASK):
todo_list.remove (nhiệm vụ)

def main ():
todo_list = create_todo_list ()
print_todo_list (todo_list)

Trong khi đúng:
lệnh = input ('nhập lệnh:')
Nếu lệnh == 'Thêm':
Task = input ('Nhập một tác vụ:')
todo_list.append (Nhiệm vụ)
lệnh elif == 'in':
print_todo_list (todo_list)
lệnh elif == 'đánh dấu':
Task = input ('Nhập một tác vụ để đánh dấu hoàn thành:')
mark_task_as_complete (todo_list, nhiệm vụ)
lệnh elif == 'thoát':
phá vỡ

Nếu __name__ == '__main__':
chủ yếu()
`` `

3. ** Một trò chơi phiêu lưu văn bản **.Mã này sẽ cho phép bạn tạo một trò chơi phiêu lưu văn bản nơi bạn có thể khám phá một thế giới, giải các câu đố và tương tác với các nhân vật.

`` `Python
def create_game ():
thế giới = {
'bắt đầu': {
'Mô tả': 'Bạn đang ở trong một hang động tối.',
'thoát': {
'Bắc': 'Rừng',
'South': 'Swamp'
}
},
'rừng': {
'Mô tả': 'Bạn đang ở trong một khu rừng rậm rạp.',
'thoát': {
'Bắc': 'Núi',
'South': 'Bắt đầu'
}
},
'đầm lầy': {
'Mô tả': 'Bạn đang ở trong một đầm lầy âm u.',
'thoát': {
'Bắc': 'Bắt đầu',
'Đông': 'Lâu đài'
}
},
'Núi': {
'Mô tả': 'Bạn đang ở trên đỉnh của một ngọn núi.',
'thoát': {}
},
'lâu đài': {
'Mô tả': 'Bạn đang ở trong một lâu đài lớn.',
'thoát': {
'Tây': 'đầm lầy'
}
}
}

trở lại thế giới

def play_game (thế giới):
current_room = world ['start']

Trong khi đúng:
in (current_room ['mô tả']))

Để thoát trong current_room ['thoát']:
print (f '{exit} -> {world [exit

next_room = input ('Bạn muốn lấy lối ra nào?')
=======================================
## 7 Python Codes for Everyday Use

Python is a powerful and versatile programming language that can be used for a wide variety of tasks. Here are seven Python codes that you can use in your everyday life:

1. **A simple calculator**. This code will allow you to perform basic arithmetic operations, such as addition, subtraction, multiplication, and division.

```python
def calculate(a, b, op):
if op == '+':
return a + b
elif op == '-':
return a - b
elif op == '*':
return a * b
elif op == '/':
return a / b
```

2. **A to-do list**. This code will allow you to create a to-do list and keep track of your tasks.

```python
def create_todo_list():
todo_list = []
while True:
task = input('Enter a task: ')
if task == '':
break
todo_list.append(task)
return todo_list

def print_todo_list(todo_list):
for task in todo_list:
print(task)

def mark_task_as_complete(todo_list, task):
todo_list.remove(task)

def main():
todo_list = create_todo_list()
print_todo_list(todo_list)

while True:
command = input('Enter a command: ')
if command == 'add':
task = input('Enter a task: ')
todo_list.append(task)
elif command == 'print':
print_todo_list(todo_list)
elif command == 'mark':
task = input('Enter a task to mark as complete: ')
mark_task_as_complete(todo_list, task)
elif command == 'quit':
break

if __name__ == '__main__':
main()
```

3. **A text adventure game**. This code will allow you to create a text adventure game where you can explore a world, solve puzzles, and interact with characters.

```python
def create_game():
world = {
'start': {
'description': 'You are in a dark cave.',
'exits': {
'north': 'forest',
'south': 'swamp'
}
},
'forest': {
'description': 'You are in a dense forest.',
'exits': {
'north': 'mountains',
'south': 'start'
}
},
'swamp': {
'description': 'You are in a murky swamp.',
'exits': {
'north': 'start',
'east': 'castle'
}
},
'mountains': {
'description': 'You are at the top of a mountain.',
'exits': {}
},
'castle': {
'description': 'You are in a grand castle.',
'exits': {
'west': 'swamp'
}
}
}

return world

def play_game(world):
current_room = world['start']

while True:
print(current_room['description'])

for exit in current_room['exits']:
print(f'{exit} -> {world[exit]["description"]}')

next_room = input('Which exit do you want to take? ')
 
Join Telegram ToolsKiemTrieuDoGroup
Back
Top