python - 我正在尝试通过电子邮件发送随机数,但我不断收到错误消息

标签 python

我的代码是一个电子邮件代码,用于通过电子邮件发送生成号码

msg = ('The number is',random.randrange(300,400),'Enjoy')

但我收到此错误:

Traceback (most recent call last):
   line 30, in <module>
    server.sendmail(fromaddr, toaddrs, msg)
  File "C:\Python34\lib\smtplib.py", line 793, in sendmail
    (code, resp) = self.data(msg)
  File "C:\Python34\lib\smtplib.py", line 532, in data
    q = _quote_periods(msg)
  File "C:\Python34\lib\smtplib.py", line 168, in _quote_periods
    return re.sub(br'(?m)^\.', b'..', bindata)
  File "C:\Python34\lib\re.py", line 175, in sub
    return _compile(pattern, flags).sub(repl, string, count)
TypeError: expected string or buffer

最佳答案

你的消息是一个元组,你可以使用字符串的format()来定义消息:

>>> import random
>>> msg = ('The number is',random.randrange(300,400),'Enjoy')
>>> print msg
('The number is', 300, 'Enjoy')
>>> msg = 'The number is {0}, Enjoy'.format(random.randrange(300, 400))
>>> msg
'The number is 325, Enjoy'

关于python - 我正在尝试通过电子邮件发送随机数,但我不断收到错误消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28262580/

相关文章:

使用 Frame 进行 Python 多处理

python - 继续并通过 : what's the difference?

python - 在 Python 中运行广义最小二乘法

python - 将 bcrypt 添加到 package.json 时,如何使用 docker node alpine Image 解决 "Could not find any Python installation to use"?

python - 在 Python 中操作子列表

python os.path.isfile() 对某些整数返回 True

python - Groupby pandas 系列中具有相同键的连续元素

python - 如何使用 python 将 .csv 文件中的行数据提取到单独的 .txt 文件中?

Python Coverage - 仅分析项目文件

python - 使用 os.path.expandvars 时防止出现空环境变量