英文:
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(" AI created by MThien \n")
sys = input(" [ System ]: My name is: ")
sys2 = 0
sys4 = 0
while True:
user = input(" [ " + str(sys) + " ]: ")
sys1 = random.choice(["Hello!", "Hello there", "Hello " + str(sys), "Sup!", "Howdy!", "Wassup!"])
sys2 = 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 = 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 ]: "+ str(sys4))
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))```
</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't intend to. Let'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>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论