为了账号安全,请及时绑定邮箱和手机立即绑定

为什么print总是报错不支持str和int连接?print有什么用?

为什么print总是报错不支持str和int连接?print有什么用?

慕盖茨4494581 2023-06-13 16:57:38
我目前正在学习 Python,所以我不知道发生了什么。import randomx=10while x>0:    print(x+'='+(random.randint(1,100)))    x-=1当我运行程序时Traceback (most recent call last):  File "C:\Users\black\.spyder-py3\temp.py", line 12, in <module>    print(x+'='+(random.randint(1,100)))TypeError: unsupported operand type(s) for +: 'int' and 'str'
查看完整描述

1 回答

?
偶然的你

TA贡献1841条经验 获得超3个赞

你x是整数,但你试图将它与字符串(等号)连接起来。


您需要将等号后的 x 和随机值都转换为字符串:


import random


x=10

while x>0:

    print(str(x)+'='+str(random.randint(1,100)))

    x-=1


查看完整回答
反对 回复 2023-06-13
  • 1 回答
  • 0 关注
  • 94 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信