收发快递python 简单打算寄包裹。发送包裹时,需要输入收件人的姓名、地址、重量、收件人姓名。包裹重量是每kg500韩元,自动计算时,请和执行结果一样编写程序。用红色书写的文字必须从键盘上输入。
时间: 2024-10-17 21:11:30 浏览: 36
在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()`函数显示包裹信息和费用。
相关问题
python收发邮件
### 使用Python实现邮件发送
为了利用Python进行邮件的发送操作,通常会采用`smtplib`库来构建SMTP客户端会话对象,这使得可以连接到服务器并登录邮箱账户从而完成邮件的发送工作[^1]。
下面是一个简单的例子展示怎样使用Python编写程序向指定地址发送一封纯文本电子邮件:
```python
import smtplib
from email.mime.text import MIMEText
from email.header import Header
sender = 'your_email@example.com'
receivers = ['receiver_email@example.com'] # 接收者列表
message = MIMEText('这是测试邮件的内容', 'plain', 'utf-8')
message['From'] = Header("发件人姓名", 'utf-8')
message['To'] = Header("收件人姓名", 'utf-8')
subject = 'Python SMTP 邮件测试'
message['Subject'] = Header(subject, 'utf-8')
try:
smtpObj = smtplib.SMTP_SSL('smtp.example.com', 465) # 创建SMTP对象,这里指定了SSL模式
smtpObj.login(sender, "password") # 登录到你自己的邮箱账号
smtpObj.sendmail(sender, receivers, message.as_string()) # 发送邮件
print ("邮件发送成功")
except Exception as e:
print (f"Error: {e}")
finally:
smtpObj.quit()
```
这段代码展示了基本的设置过程以及异常处理机制以确保即使发生错误也能正常关闭与SMTP服务器之间的连接。
对于更复杂的需求比如HTML格式邮件或是带有附件的情况,则需要引入其他MIME类型的支持,并调整相应的编码方式。
---
### 使用Python实现邮件接收
当涉及到通过Python读取或下载已收到的邮件时,一般会选择IMAP协议而不是POP3因为前者允许用户在不删除原始消息的情况下查看远程邮箱中的内容。为此目的服务的一个流行第三方库叫做`imaplib`.
以下是获取最新未读邮件主题的一段示范脚本:
```python
import imaplib
import email
from email.header import decode_header
def fetch_unread_emails(email_user, password):
mail = imaplib.IMAP4_SSL('imap.example.com')
mail.login(email_user,password)
status, messages = mail.select('INBOX')
response_code, data = mail.search(None,'UNSEEN')
unread_msg_nums = data[0].split()
latest_unread_subjects = []
for num in unread_msg_nums[-1:-6:-1]:
typ, msg_data = mail.fetch(num, '(RFC822)')
for response_part in msg_data:
if isinstance(response_part, tuple):
msg = email.message_from_bytes(response_part[1])
subject, encoding = decode_header(msg["Subject"])[0]
if isinstance(subject, bytes):
try:
subject = subject.decode(encoding or 'utf-8')
except UnicodeDecodeError:
continue
latest_unread_subjects.append(subject)
fetch_unread_emails('your_email@example.com','password')
print(latest_unread_subjects)
```
此函数将返回最近五封未阅读过的电邮的主题行作为字符串数组输出.
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`对象,根据具体需求提取发件人、主题、正文等信息。
阅读全文
相关推荐















