我写了这个程序
#!/usr/bin/env python
"""
"""
import random
def CollectStrings():
string_list = []
while True:
string = raw_input("What is your question: (enter to quit): ")
if not string:
return string_list
string_list.append(string)
def ChooseStrings(how_many):
string_list = CollectStrings()
chosen = random.sample(string_list, how_many)
chosen.sort()
return ', '.join(chosen)
print ChooseStrings (3)
但我需要让这个程序随机回答问题,比如8ball。 我该怎么做?
答案 0 :(得分:2)
将所有答案添加到列表中,然后使用random.choices获取随机答案:
import random
answers = [
"The answer lies in your heart",
"I do not know",
"Almost certainly",
"No",
"Yes",
"Why do you need to ask?",
"Go away. I do not wish to answer at this time.",
"Time will only tell",
]
print random.choice(answers) // Print random answer
答案 1 :(得分:0)
l=['answers']
#create a list of answers
q=['questions']
#create a list of questions
import random
x=input('your question')
if x in q:
print(random.choice(q))