项目1:计算器
项目简介
一个基础的命令行计算器,可以帮助用户进行加、减、乘、除等基本运算。
实现代码(Python)
def add(x, y):
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."
else:
return x / y
while True:
print("Options:")
print("1.Add")
print("2.Subtract")
print("3.Multiply")
print("4.Divide")
print("5.Quit")
choice = input("Enter choice(1/2/3/4/5): ")
if choice in ('1', '2', '3', '4'):
num1 = float(input("Enter first number: "))
num2 = float(input("Enter second number: "))
if choice == '1':
print("Result:", add(num1, num2))
elif choice == '2':
print("Result:", subtract(num1, num2))
elif choice == '3':
print("Result:", multiply(num1, num2))
elif choice == '4':
print("Result:", divide(num1, num2))
elif choice == '5':
break
else:
print("Invalid Input")
项目2:猜数字游戏
项目简介
一个简单的猜数字游戏,计算机随机生成一个数字,用户尝试猜测这个数字。
实现代码(Python)
import random
def guess_number():
number_to_guess = random.randint(1, 100)
guess = 0
while guess != number_to_guess:
guess = int(input("Enter a guess between 1 and 100: "))
if guess < number_to_guess:
print("Too low!")
elif guess > number_to_guess:
print("Too high!")
else:
print("Congratulations! You guessed it right!")
guess_number()
项目3:待办事项列表
项目简介
一个简单的待办事项列表,用户可以添加、删除和查看待办事项。
实现代码(Python)
def add_task(tasks):
task = input("Enter a new task: ")
tasks.append(task)
return tasks
def remove_task(tasks):
task = input("Enter a task to remove: ")
tasks.remove(task)
return tasks
def view_tasks(tasks):
print("Your tasks:")
for task in tasks:
print(task)
tasks = []
while True:
print("1. Add Task")
print("2. Remove Task")
print("3. View Tasks")
print("4. Quit")
choice = input("Enter choice: ")
if choice == '1':
tasks = add_task(tasks)
elif choice == '2':
tasks = remove_task(tasks)
elif choice == '3':
view_tasks(tasks)
elif choice == '4':
break
else:
print("Invalid Input")
项目4:温度转换器
项目简介
一个简单的温度转换器,可以将摄氏度和华氏度相互转换。
实现代码(Python)
def celsius_to_fahrenheit(celsius):
return (celsius * 9/5) + 32
def fahrenheit_to_celsius(fahrenheit):
return (fahrenheit - 32) * 5/9
celsius = float(input("Enter temperature in Celsius: "))
fahrenheit = celsius_to_fahrenheit(celsius)
print(f"Temperature in Fahrenheit: {fahrenheit}")
fahrenheit = float(input("Enter temperature in Fahrenheit: "))
celsius = fahrenheit_to_celsius(fahrenheit)
print(f"Temperature in Celsius: {celsius}")
项目5:猜字母游戏
项目简介
一个简单的猜字母游戏,计算机随机生成一个字母,用户尝试猜测这个字母。
实现代码(Python)
import random
def guess_letter():
letter_to_guess = random.choice("abcdefghijklmnopqrstuvwxyz")
guess = ""
while guess != letter_to_guess:
guess = input("Guess a letter: ")
if guess.lower() == letter_to_guess.lower():
print("Congratulations! You guessed it right!")
break
else:
print("Wrong guess! Try again.")
guess_letter()
项目6:简易文本编辑器
项目简介
一个简单的文本编辑器,用户可以输入文本,然后保存和查看文本内容。
实现代码(Python)
def create_file(filename):
with open(filename, 'w') as file:
file.write("")
def open_file(filename):
with open(filename, 'r') as file:
print(file.read())
def save_file(filename):
content = input("Enter text to save: ")
with open(filename, 'w') as file:
file.write(content)
filename = input("Enter filename: ")
create_file(filename)
while True:
print("1. Open File")
print("2. Save File")
print("3. Quit")
choice = input("Enter choice: ")
if choice == '1':
open_file(filename)
elif choice == '2':
save_file(filename)
elif choice == '3':
break
else:
print("Invalid Input")
项目7:简易音乐播放器
项目简介
一个简单的音乐播放器,可以播放本地音乐文件。
实现代码(Python)
import os
import pygame
def play_music():
pygame.mixer.init()
pygame.mixer.music.load("your_song.mp3")
pygame.mixer.music.play()
def pause_music():
pygame.mixer.music.pause()
def resume_music():
pygame.mixer.music.unpause()
def stop_music():
pygame.mixer.music.stop()
while True:
print("1. Play Music")
print("2. Pause Music")
print("3. Resume Music")
print("4. Stop Music")
print("5. Quit")
choice = input("Enter choice: ")
if choice == '1':
play_music()
elif choice == '2':
pause_music()
elif choice == '3':
resume_music()
elif choice == '4':
stop_music()
elif choice == '5':
break
else:
print("Invalid Input")
项目8:简易日历
项目简介
一个简单的日历,可以显示当前月份的日期和星期。
实现代码(Python)
import calendar
def display_calendar():
month = int(input("Enter month (1-12): "))
year = int(input("Enter year: "))
month_calendar = calendar.month(year, month)
print(month_calendar)
display_calendar()
项目9:简易天气查询
项目简介
一个简单的天气查询工具,可以查询指定地点的天气信息。
实现代码(Python)
import requests
def get_weather():
city = input("Enter city name: ")
api_key = "your_api_key"
url = f"http://api.openweathermap.org/data/2.5/weather?q={city}&appid={api_key}"
response = requests.get(url)
data = response.json()
weather = data['weather'][0]['description']
temperature = data['main']['temp']
print(f"Weather in {city}: {weather} with temperature {temperature}°C")
get_weather()
项目10:简易待办事项提醒
项目简介
一个简单的待办事项提醒工具,可以设置提醒时间,并在指定时间提醒用户。
实现代码(Python)
import time
import datetime
def set_reminder():
reminder_time = input("Enter reminder time (HH:MM:SS): ")
current_time = datetime.datetime.now()
time_to_wait = datetime.datetime.strptime(reminder_time, "%H:%M:%S") - current_time
if time_to_wait.total_seconds() < 0:
print("Invalid time!")
return
time.sleep(time_to_wait.total_seconds())
print("Reminder: Don't forget your task!")
set_reminder()
项目11:简易文本搜索器
项目简介
一个简单的文本搜索器,可以在文本中搜索指定关键词。
实现代码(Python)
def search_text():
text = input("Enter text: ")
keyword = input("Enter keyword to search: ")
if keyword in text:
print("Keyword found!")
else:
print("Keyword not found!")
search_text()
项目12:简易聊天机器人
项目简介
一个简单的聊天机器人,可以根据用户输入的指令给出相应的回复。
实现代码(Python)
def chatbot():
print("Hello! I'm a simple chatbot. Ask me anything!")
while True:
user_input = input("You: ")
if user_input == "exit":
print("Chatbot: Goodbye!")
break
if "how are you" in user_input.lower():
print("Chatbot: I'm fine, thank you! How about you?")
elif "what's your name" in user_input.lower():
print("Chatbot: I'm a simple chatbot.")
else:
print("Chatbot: I'm not sure how to answer that.")
chatbot()
项目13:简易待办事项管理器
项目简介
一个简单的待办事项管理器,可以添加、删除和查看待办事项。
实现代码(Python)
def add_task(tasks):
task = input("Enter a new task: ")
tasks.append(task)
return tasks
def remove_task(tasks):
task = input("Enter a task to remove: ")
tasks.remove(task)
return tasks
def view_tasks(tasks):
print("Your tasks:")
for task in tasks:
print(task)
tasks = []
while True:
print("1. Add Task")
print("2. Remove Task")
print("3. View Tasks")
print("4. Quit")
choice = input("Enter choice: ")
if choice == '1':
tasks = add_task(tasks)
elif choice == '2':
tasks = remove_task(tasks)
elif choice == '3':
view_tasks(tasks)
elif choice == '4':
break
else:
print("Invalid Input")
项目14:简易时间管理器
项目简介
一个简单的
…(由于篇幅限制,这里仅展示部分项目。请根据需要继续阅读或访问完整文章链接获取全部内容。)
