首页 > 解决方案 > 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

这个程序的意思是输出绿色的随机数

标签: pythoncolors

解决方案


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库。


推荐阅读