收发快递python 简单打算寄包裹。发送包裹时,需要输入收件人的姓名、地址、重量、收件人姓名。包裹重量是每kg500韩元,自动计算时,请和执行结果一样编写程序。用红色书写的文字必须从键盘上输入。
时间: 2024-10-17 22:11:30 浏览: 16
在Python中,你可以创建一个简单的函数来模拟寄送包裹的过程。首先,你需要获取用户输入的信息,然后进行费用计算并打印出结果。以下是一个简单的示例:
```python
def send_package():
# 输入信息
recipient_name = input("请输入收件人的姓名: ")
recipient_address = input("请输入收件人的地址: ")
weight_kg = float(input("请输入包裹的重量(kg): "))
# 计算费用
cost_per_kg = 500 # 韩元每公斤
total_cost = weight_kg * cost_per_kg
# 打印结果
print(f"您要寄送的包裹信息如下:")
print(f"收件人: {recipient_name}")
print(f"地址: {recipient_address}")
print(f"重量: {weight_kg} kg")
print(f"费用: {total_cost} 韩元")
# 调用函数
send_package()
```
在这个脚本中,我们首先提示用户输入姓名、地址和重量,并将它们存储在变量中。接着,我们计算总费用,然后用`print()`函数显示包裹信息和费用。
相关问题
2024 python 脚本收发邮件
在Python中,你可以使用内置的`smtplib`和`email`库来编写脚本来发送和接收电子邮件。以下是基本步骤:
**发送邮件(SMTP)**:
```python
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
# 邮件配置
smtp_server = "your-smtp-server.com"
port = 587 # 或者465 for SSL/TLS
username = "your-email@example.com"
password = "your-password"
def send_email(recipient, subject, message):
msg = MIMEMultipart()
msg['From'] = username
msg['To'] = recipient
msg['Subject'] = subject
body = MIMEText(message, 'plain')
msg.attach(body)
server = smtplib.SMTP(smtp_server, port)
server.starttls() # 如果需要加密
server.login(username, password)
text = msg.as_string()
server.sendmail(username, recipient, text)
server.quit()
# 示例用法
send_email("recipient@example.com", "测试邮件", "这是一封测试邮件")
```
**接收邮件(POP3 / IMAP)**:
```python
import imaplib
from email.parser import BytesParser
# 邮箱配置
imap_server = "your-imap-server.com"
imap_port = 993 # or 143 for non-SSL
username = "your-email@example.com"
password = "your-password"
def fetch_emails():
mail = imaplib.IMAP4_SSL(imap_server) if imap_port == 993 else imaplib.IMAP4(imap_server)
mail.login(username, password)
mail.select('inbox') # 根据邮箱选择合适的标签
status, messages = mail.search(None, 'ALL')
latest_email_id = messages[0].split()[-1] # 获取最新邮件ID
typ, data = mail.fetch(latest_email_id, '(RFC822)')
raw_email = data[0][1]
email_message = BytesParser().parsebytes(raw_email)
# 解析邮件内容
...
mail.close()
mail.logout()
fetch_emails()
```
请注意,实际应用中你需要替换上面的服务器地址、端口和用户名密码。此外,处理邮件内容通常会涉及到解析`email.message.Message`对象,根据具体需求提取发件人、主题、正文等信息。
python怎么实现邮件收发功能的代码
实现邮件收发功能的代码,需要使用 Python 的 `smtplib` 和 `email` 库。以下是一个简单的例子:
```python
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.mime.application import MIMEApplication
# 发件人邮箱账号
sender = 'your_email@example.com'
# 发件人邮箱密码
password = 'your_email_password'
# 收件人邮箱账号
receiver = 'recipient_email@example.com'
# 创建邮件对象
msg = MIMEMultipart()
# 设置邮件主题、发件人、收件人
subject = 'Python Email Test'
msg['Subject'] = subject
msg['From'] = sender
msg['To'] = receiver
# 添加邮件正文
body = 'This is a test email sent from Python.'
msg.attach(MIMEText(body, 'plain'))
# 添加附件
filename = 'test.txt'
with open(filename, 'rb') as f:
attachment = MIMEApplication(f.read(), _subtype='txt')
attachment.add_header('Content-Disposition', 'attachment', filename=filename)
msg.attach(attachment)
# 发送邮件
try:
smtpObj = smtplib.SMTP('smtp.example.com', 25)
smtpObj.login(sender, password)
smtpObj.sendmail(sender, receiver, msg.as_string())
print('邮件发送成功')
except smtplib.SMTPException as e:
print('邮件发送失败:', e)
finally:
smtpObj.quit()
```
需要将 `your_email@example.com` 和 `your_email_password` 替换为实际的发件人邮箱地址和密码,将 `recipient_email@example.com` 替换为实际的收件人邮箱地址,将 `smtp.example.com` 替换为实际的 SMTP 服务器地址。此外,如果需要添加附件,需要将 `test.txt` 替换为实际的附件文件名。
阅读全文