# 对话数据 dialogue = [ ["初始对话", "a.跳转至A", "b.跳转至B", "c.结束"], ["对话A", "a.跳转至C", "b.跳转至B", "c.跳转至D1(结束)"], ["对话B", "a.跳转至C", "b.跳转至A", "c.跳转至D2(结束)"], ["对话C", "a.跳转至B", "b.跳转至A", "c.跳转至D3(结束)"], ["对话D1", "仅内容,无回答,结束"], ["对话D2", "仅内容,无回答,结束"], ["对话D3", "仅内容,无回答,结束"] ] # 初始化游戏 current_dialogue = 0 # 游戏循环 while True: # 获取当前对话 current = dialogue[current_dialogue] # 打印对话内容 print(current[0]) # 打印回答选项 for i in range(1, len(current)): print(current[i]) # 获取玩家输入 choice = input("请输入选项:") # 根据玩家选项进行跳转 if choice.isdigit(): choice = int(choice) if choice >= 1 and choice <= len(current) - 1: # 更新当前对话 current_dialogue = ord(current[choice]) - ord('A') if current_dialogue >= len(dialogue): break else: print("无效的选项!") else: print("请输入有效的选项!") 应该怎么输入
时间: 2024-04-23 13:28:26 浏览: 94
你可以根据每个对话选项前的字母来输入选项。例如,对于初始对话,你可以输入'a'选择跳转至A,输入'b'选择跳转至B,输入'c'选择结束。对话A、B、C等后续对话也是类似的方式进行选择。请注意,输入选项时不区分大小写。
以下是一个示例交互的过程:
```
初始对话
a.跳转至A
b.跳转至B
c.结束
请输入选项:a
对话A
a.跳转至C
b.跳转至B
c.跳转至D1(结束)
请输入选项:b
对话B
a.跳转至C
b.跳转至A
c.跳转至D2(结束)
请输入选项:c
对话D2
仅内容,无回答,结束
```
你可以根据需要选择不同的选项进行对话。如果输入无效的选项,系统会给出相应的提示。
相关问题
import socket import traceback import sys host = '172.22.102.20' port = 51423 s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) s.bind((host, port)) s.listen(1) ClientSock, ClientAddr = s.accept() while 1: try: buf = ClientSock.recv(1024) if len(buf): print("he say: " + buf.decode()) data = input("I say: ").encode() ClientSock.sendall(data) except: print("Dialogue Over") ClientSock.close() sys.exit(0)是否有缩进错误
是的,有缩进错误。while循环下面的try语句块中,if语句和print语句需要再缩进一级,如下所示:
```
while 1:
try:
buf = ClientSock.recv(1024)
if len(buf):
print("he say: " + buf.decode())
data = input("I say: ").encode()
ClientSock.sendall(data)
except:
print("Dialogue Over")
ClientSock.close()
sys.exit(0)
```
import socket import traceback import sys host = '172.22.102.20' port = 51423 s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) s.bind((host, port)) s.listen(1) ClientSock, ClientAddr = s.accept() while 1: try: buf = ClientSock.recv(1024) if len(buf): print("he say: " + buf.decode()) data = input("I say: ").encode() ClientSock.sendall(data) except: print("Dialogue Over") ClientSock.close() sys.exit(0)这个代码有 File "C:\Users\Administrator\PycharmProjects\chat\serve.py", line 18 if len(buf): ^ IndentationError: unexpected indent
这个错误是因为 if 语句下面的代码缩进不正确。你需要检查一下代码中的缩进是否正确,建议使用 4 个空格来缩进代码。以下是修复后的代码:
```
import socket
import traceback
import sys
host = '172.22.102.20'
port = 51423
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind((host, port))
s.listen(1)
ClientSock, ClientAddr = s.accept()
while 1:
try:
buf = ClientSock.recv(1024)
if len(buf):
print("he say: " + buf.decode())
data = input("I say: ").encode()
ClientSock.sendall(data)
except:
print("Dialogue Over")
ClientSock.close()
sys.exit(0)
```
阅读全文