随机调用一个函数

时间:2018-01-10 05:51:05

标签: python function random call

我正在制作这个聊天响应程序,我的目标是让计算机随机从数据库中挑出一个问题并能够根据输入的答案做出响应。没有错误通知,但问题是它要求的第一个问题不会返回答案,它会询问列表中的所有问题,而不仅仅是一个问题。我该如何解决?谢谢你的帮助!

import random

x=input("What is your name? ")

def feeling():
    return input("How are you feeling right now " +str(x)+"? ")

def homesick():
    return input("Do you miss your home? ")

def miss():
    return input("Who do you miss?")

prompts = [feeling,homesick,miss]
response = random.choice(prompts)()

if feeling()==("tired"):
    Tired=['I wish I can make you feel better.','I hope school is not making you feel stressed.','You deserve the right to relax.']
    print(random.choice(Tired))

if homesick()==("yes"):
    yes=["Don't worry, you will be home soon......",'I am protecting your family and loved ones, trust me on this.',"Your kingdoms has been waiting for a long time, they'd forgiven your mistakes"]
    print(random.choice(yes))

if miss()==("my mom"):
    print("Mom will be in town soon")

1 个答案:

答案 0 :(得分:1)

input()返回一个字符串,而不是一个函数对象。

因此question是一个字符串列表,您无法调用str

之类的random.choice(question)()对象

如果您想提示随机输入,那么您可以这样做,例如

prompts = ["How are you feeling right now " +str(x)+"? "]
response = input(random.choice(prompts))

然后,您需要使用response变量

来创建if语句

如果您想要随机功能,则需要def

def miss():
    return input("What do you miss? ")

prompts = [miss]
response = random.choice(prompts)()

顺便说一句,你应该使用while循环,而不是调用函数本身