python - TypeError:只能将str(不是“int”)连接到str python
问题描述
这是我的代码
import time, random
import colorama
from colorama import Fore
colorama.init(autoreset=True)
while True:
random = random.randint(0,100)
time.sleep(0.1)
print(Fore.GREEN + random)
但我收到一个错误:
print(Fore.GREEN + random)
TypeError: can only concatenate str (not "int") to str
这个程序的意思是输出绿色的随机数
解决方案
import time, random
import colorama
from colorama import Fore
colorama.init(autoreset=True)
while True:
random_num = random.randint(0,100)
time.sleep(0.1)
print(Fore.GREEN + str(random_num))
你忘了使用str
所以发生的事情是你试图做的:str + int
这不是反抗。此外,您将随机数变量命名为random
并覆盖了random
库。
推荐阅读
- python - 在 KML 中引用本地图像会导致行为不一致
- python - 无法使用正确的 pagetoken 查询参数正确表达其余请求
- javascript - 通过脚本将特定文件格式从文件夹导入 Illustrator
- azure-logic-apps - 在逻辑应用中验证 X12 消息
- php - 试图获取非对象属性的 Guzzle 响应
- arangodb - ArangoDB 如何确定它有多少可用 RAM?
- java - 为什么我对两个整数值的比较不起作用?
- arrays - AppScript:“数据中的列数与范围内的列数不匹配。” setValues 方法没有正确读取数组?
- python - 防止 Pandas read_csv 将 NA 解释为 NaN 但将 NaN 保留为空值
- c - 格式化字符串漏洞实验室