# Python练手小项目

5 篇文章 1 订阅

#### 计算器

# 定义加法函数
return x + y

# 定义减法函数
def subtract(x, y):
return x - y

# 定义乘法函数
def multiply(x, y):
return x * y

# 定义除法函数
def divide(x, y):
if y == 0:
return "Error! Division by zero."  # 防止除以零
return x / y

# 打印操作选项
print("Select operation:")
print("2. Subtract")
print("3. Multiply")
print("4. Divide")

# 获取用户选择的操作
choice = input("Enter choice(1/2/3/4): ")

# 获取用户输入的两个数字
num1 = float(input("Enter first number: "))
num2 = float(input("Enter second number: "))

# 根据用户选择的操作调用相应的函数
if choice == '1':
print(f"{num1} + {num2} = {add(num1, num2)}")
elif choice == '2':
print(f"{num1} - {num2} = {subtract(num1, num2)}")
elif choice == '3':
print(f"{num1} * {num2} = {multiply(num1, num2)}")
elif choice == '4':
print(f"{num1} / {num2} = {divide(num1, num2)}")
else:
print("Invalid input")  # 处理无效输入


#### 猜数字游戏

import random  # 导入随机数模块

# 生成一个1到100之间的随机数
number_to_guess = random.randint(1, 100)
attempts = 0  # 初始化尝试次数

print("Guess the number between 1 and 100")

while True:
guess = int(input("Enter your guess: "))  # 获取用户猜测的数字
attempts += 1  # 尝试次数加一
if guess < number_to_guess:
print("Too low!")  # 提示猜的数字太小
elif guess > number_to_guess:
print("Too high!")  # 提示猜的数字太大
else:
print(f"Congratulations! You've guessed the number in {attempts} attempts.")  # 猜对了
break  # 结束循环


#### 简单的记事本

notes = []  # 初始化一个空列表来存储笔记

# 添加笔记的函数
note = input("Enter your note: ")
notes.append(note)

# 查看所有笔记的函数
def view_notes():
for i, note in enumerate(notes, 1):
print(f"{i}. {note}")

# 删除笔记的函数
def delete_note():
view_notes()  # 先显示所有笔记
note_index = int(input("Enter the note number to delete: ")) - 1
if 0 <= note_index < len(notes):
del notes[note_index]
print("Note deleted!")
else:
print("Invalid note number")  # 处理无效输入

# 主循环
while True:
print("2. View Notes")
print("3. Delete Note")
print("4. Exit")

choice = input("Enter your choice: ")

if choice == '1':
elif choice == '2':
view_notes()
elif choice == '3':
delete_note()
elif choice == '4':
break  # 退出循环
else:
print("Invalid choice. Please try again.")  # 处理无效选择


#### 简单的联系人管理系统

contacts = {}  # 初始化一个空字典来存储联系人

# 添加联系人的函数
name = input("Enter contact name: ")
phone = input("Enter contact phone number: ")
contacts[name] = phone

# 查看所有联系人的函数
def view_contacts():
for name, phone in contacts.items():
print(f"Name: {name}, Phone: {phone}")

# 删除联系人的函数
def delete_contact():
name = input("Enter the name of the contact to delete: ")
if name in contacts:
del contacts[name]
print("Contact deleted!")
else:

# 主循环
while True:
print("2. View Contacts")
print("3. Delete Contact")
print("4. Exit")

choice = input("Enter your choice: ")

if choice == '1':
elif choice == '2':
view_contacts()
elif choice == '3':
delete_contact()
elif choice == '4':
break  # 退出循环
else:
print("Invalid choice. Please try again.")  # 处理无效选择


#### 简单的待办事项列表

todo_list = []  # 初始化一个空列表来存储待办事项

# 添加任务的函数

# 查看所有任务的函数
for i, task in enumerate(todo_list, 1):

# 删除任务的函数
if 0 <= task_index < len(todo_list):
else:

# 主循环
while True:
print("4. Exit")

choice = input("Enter your choice: ")

if choice == '1':
elif choice == '2':
elif choice == '3':
elif choice == '4':
break  # 退出循环
else:
print("Invalid choice. Please try again.")  # 处理无效选择

• 24
点赞
• 136
收藏
觉得还不错? 一键收藏
• 4
评论
09-29 269
07-19 1万+
10-13 3774
09-05 1167
09-09 416

1.余额是钱包充值的虚拟货币，按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载，可以购买VIP、付费专栏及课程。