如何修复聊天机器人卡住的问题。

huangapple go评论99阅读模式
英文:

how to fix when chatbot hangs

问题

我正在基于Roblox中的“Talking NPC”游戏创建一个聊天机器人。

在我键入“hello”后,它崩溃了。

错误代码

跟踪 (most recent call last):
文件 "/data/user/0/ru.iiec.pydroid3/files/accomp_files/iiec_run/iiec_run.py", 第 31 行, 在 中:
start(fakepyfile,mainpyfile)
文件 "/data/user/0/ru.iiec.pydroid3/files/accomp_files/iiec_run/iiec_run.py", 第 30 行, 在 start 中:
exec(open(mainpyfile).read(), main.dict)
文件 "", 第 21 行, 在 中:
TypeError: 只能将 str(而不是 "int")与 str 连接

代码

import time
import random

print("由MThien创建的人工智能\n")

sys = input(" [ 系统 ]: 我的名字是:")

sys2 = 0
sys4 = 0

while True:
    user  = input(" [ " + str(sys) + " ]: ")

    sys1 = random.choice(["你好!", "你好!", "你好 " + str(sys), "嗨!", "你好!", "你好!"])
    sys2 = random.choice(["打错了,对不起。", "抱歉,我的访客刚刚出现了精神崩溃。"])
    sys3 = random.choice(["什么?", "?", "对不起?", "我不明白", "我不明白。", "抱歉,我的英语不好。", "抱歉,我的智商仍然很低。"])
    sys4 = random.choice(["...", "停止吧。", "你已经对我说了几次你好了。", "问候我已经足够了。"])

    if user == "hello":
        print(" [ 人工智能 ]: " + str(sys1))
        sys4 += 1
        if sys4 >= 5:
            print(" [ 人工智能 ]: " + str(sys4)) 
    elif user == "/call":
        print(" [ 电话 ]: *拨号*")
        time.sleep(2)
        print(" [ 电话 ]: 你拨打的号码未注册。")
        time.sleep(1)
        print(" [ 电话 ]: *通话结束*")
    elif user == "/call 911":
        print(" [ 电话 ]: *拨号*")
        time.sleep(2)
        print(" [ 电话 ]: 911,你有什么紧急情况?")
        time.sleep(1)
        print(" [ 人工智能 ]: " + str(sys1))
        time.sleep(1)
        print(" [ 电话 ]: *通话结束*")
        sys2 += 1
        if sys2 == 3:
            time.sleep(3)
            print(" [ FBI ]: FBI,开门!")
            time.sleep(5)
            print(" [ 系统 ]: 你被从这个体验中踢出来了:你被逮捕了!")
            break
    else:
        print(" [ 人工智能 ]: " + str(sys3))
英文:

I am creating a chatbot based on The Talking NPC game in Roblox

it crashes after i type hello

error code
>Traceback (most recent call last):
File "/data/user/0/ru.iiec.pydroid3/files/accomp_files/iiec_run/iiec_run.py", line 31, in <module>
start(fakepyfile,mainpyfile)
File "/data/user/0/ru.iiec.pydroid3/files/accomp_files/iiec_run/iiec_run.py", line 30, in start
exec(open(mainpyfile).read(), main.dict)
File "<string>", line 21, in <module>
TypeError: can only concatenate str (not "int") to str

code

import random

print(&quot; AI created by MThien \n&quot;)

sys = input(&quot; [ System ]: My name is: &quot;)

sys2 = 0
sys4 = 0

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

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


</details>


# 答案1
**得分**: 1

你在不打算的情况下覆盖了你的值。让我们以用户输入的 `hello` 为例。在到达这个语句之前,你有两个语句:

sys4 = 0
sys4 = random.choice(["...", "Stop it.", "You've already said hello to me several times.", "That's enough for greeting me."])

当你到达第二个语句时,`sys4` 现在是一个字符串。然后你执行 `sys4 += 1`,你试图将 `1` 添加到一个字符串上。Python 不支持这样做,很明显你想要将 `1` 添加到 `sys4` 的整数版本而不是字符串版本。然而,`sys4` 不再存在为整数。

适当命名你的变量,问题应该就会消失。

<details>
<summary>英文:</summary>

You are overwriting your values when you likely don&#39;t intend to. Let&#39;s take a user input of `hello` for example. Prior to reaching this statement, you have two statements being

sys4 = 0
sys4 = random.choice(["...", "Stop it.", "You've already said hello to me several times.", "That's enough for greeting me."])

Once you hit that second statement, sys4 is now a string. You then get to `sys4 += 1`, where you are trying to add `1` to a string. Python does not support this, and it is obvious you were trying to add `1` to the integer version of `sys4` and not the string version. However, `sys4` no longer exists as an integer.

Properly name your variables and the problem should go away.

Also, the tags for `android` `artifical-intelligence`, and `roblox` are not relevant to this question.

</details>



huangapple
  • 本文由 发表于 2023年3月1日 08:42:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/75598637.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定