Search code examples
pythonfunctionrandomcall

Call one single function randomly


I am making this chat-respond program, my goal is for the computer to pick a question out of the database randomly and able to respond base on the input answer. There are no error notices but the problem is that the first question it asks does not return an answer and it asks all the question on the list, not just one. How do I fix this? Thanks for the help!

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")

Solution

  • input() returns a string, not a function object.

    Therefore question is a list of strings, and you cannot call a str object like random.choice(question)()

    If you want to prompt for random input, then you would do this, for example

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

    Then, you need to make your if statements use response variable

    If you want random functions, you will need a def

    def miss():
        return input("What do you miss? ")
    
    prompts = [miss]
    response = random.choice(prompts)()
    

    By the way, you should loop using a while, not calling the function itself