我有一個30個字符串的列表。我想使用隨機模塊的選擇方法,並從它們所存儲的列表中生成一個新字符串。我不想重複任何字符串,並且我想打印所有唯一字符串一次。我試圖做一個聊天機器人,但我只能得到1串打印一遍又一遍我每次運行該程序random.choice與清單
print("you are speaking with Donald Trump. If you wish to finish your conversation at any time, type good bye")
greetings = ["hello", "hey", "what's up ?", "how is it going?", ]
#phrase_list = ["hello", "the wisdom you seek is inside you", "questions are more important than answers"]
random_greeting = random.choice(greetings)
print(random_greeting)
open_article = open(filePath, encoding= "utf8")
read_article = open_article.read()
toks = read_article.split('"')
random_tok = random.choice(toks)
conversation_length = 0
responses = ''
while True: #getting stuck in infinite loops get out and make interative
user_response = input(" ")
if user_response != "" or user_response != "good bye":
responses = responses + user_response
conversation_length = conversation_length + 1
while conversation_length < 31:
print(random_tok)
if conversation_length >= 31:
print("bye bye")
能否請您包括你的代碼片段呢? – Unni
我添加了上面的代碼。它不顯示它,但我在程序開始時隨機導入,只是沒有粘貼 – reubs