2012-02-09 131 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