2012-02-09 129 views
-1

我写这PROGRAMMPython的随机应答程序

#!/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) 

但我需要让这个程序随机回答的问题,比如一个很可能很可能。 我该怎么做?

+3

你应该改一下你的问题具体的东西。 “你能为我写这个程序吗”并不具体。你对你的计划有什么了解?你认为这个问题在哪里?你有什么尝试? – 2012-02-09 09:36:57

回答

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