You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

基于Roblox Talking NPC的聊天机器人输入hello崩溃的解决方法

解决Roblox《Talking NPC》聊天机器人的TypeError崩溃问题

错误原因分析

报错信息显示TypeError: can only concatenate str (not "int") to str,根源在于变量名冲突:

  • 初始定义sys4 = 0作为记录"hello"输入次数的整数计数器
  • 但在while循环内,执行sys4 = random.choice(["...", ...])将sys4重新赋值为字符串类型的回复内容
  • 后续执行sys4 += 1时,试图把字符串和整数相加,触发类型错误

修复方案

1. 分离变量职责

将计数器变量和随机回复变量分开命名,避免类型覆盖:

  • 保留sys4作为整数计数器
  • 新增sys4_reply存储针对重复hello的随机回复内容
  • 同理修复sys2的变量冲突问题,新增sys2_reply存储对应回复

2. 补充缺失的模块导入

代码中使用了time.sleep()但未导入time模块,需添加导入语句。

修改后的完整代码

import random
import time  # 补充缺失的time模块导入

print(" AI created by MThien \n")

sys = input(" [ System ]: My name is: ")

sys2 = 0  # 记录911呼叫次数的计数器
sys4 = 0  # 记录hello输入次数的计数器

while True:
    user  = input(" [ " + str(sys) + " ]: ")
    
    sys1 = random.choice(["Hello!", "Hello there", "Hello " + str(sys), "Sup!", "Howdy!", "Wassup!"])
    sys2_reply = random.choice(["Wrong number, sorry.", "Apologize, my visitor is just having a mental breakdown."])
    sys3 = random.choice(["What?", "?", "Sorry?", "I don't understand", "I don't get it.", "Sry my English is bad.", "Sry my IQ is still low."])
    sys4_reply = random.choice(["...", "Stop it.", "You've already said hello to me several times.", "That's enough for greeting me."])

    if user == "hello":
        print(" [ AI ]: " + str(sys1))
        sys4 += 1
        if sys4 >= 5:
            print(" [ AI ]: "+ sys4_reply)
    elif user == "/call":
        print(" [ Telephone ] : *dialing*")
        time.sleep(2)
        print(" [ Telephone ] : The number you're calling isn't registered.")
        time.sleep(1)
        print(" [ Telephone ] : *call ended*")
    elif user == "/call 911":
        print(" [ Telephone ] : *dialing*")
        time.sleep(2)
        print(" [ Telephone ] : 911, what's your emergency?")
        time.sleep(1)
        print(" [ AI ]: "+ str(sys1))
        time.sleep(1)
        print(" [ Telephone ] : *call ended*")
        sys2 += 1
        if sys2 == 3:
            time.sleep(3)
            print(" [ FBI ] : FBI, OPEN UP!")
            time.sleep(5)
            print(" [ System ]: You were kicked from this experience: You got arrested!")
            break
    else:
        print(" [ AI ]: " + str(sys3))

关键修改点说明

  • 拆分了计数器变量与回复内容变量,确保计数器始终为整数类型,避免类型冲突
  • 补充import time语句,解决time.sleep()未定义的报错
  • 修正了原代码中sys2同样存在的变量覆盖问题

内容的提问来源于stack exchange,提问作者MThien

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.29 14:17:52