Ask coding questions

← Back to all posts
Python Code HS Question
SimplyMiah (1)

Hey guys! Can anybody please help me? I need to make a program where the user asks a question and the program responds with “yes” “no” “maybe” and other random phrases, kind of like a fortune teller. I have imported random already and created a list with the possible outputs and a print function. I’m just curious, is there a way I can include “def (parameters): ” with code in this function? Like is there anything I could add to my program so I can use a function like this? Thank you for your time :)

IMayBeMe (335)

So adding parameters pretty straightforward assuming you already know how to do the part with random. So your basic program probably looks something like this currently:

import random

def function_name():
  print(random.choice(['yes', 'no', 'maybe']))

or using a variable for the list like this:

import random

def function_name():
  options = ['yes', 'no', 'maybe']

Now if you want to use a parameter all you need to do is to pass a parameter instead of declaring a variable like this:

import random

def function_name(options):

function_name(['yes', 'no', 'maybe'])
CookieSnowOwl (41)

from random import choices
def(parameters, parameterstwo):
list = parameterstwo
x = choices(list, weights=parameters)

CookieSnowOwl (41)

So what you can do is
from random import choices
choices syntax is
choices(list, optional weights=args, optional cum_weights = args)
go look up the random documentary too

LukeWright (114)

I don't understand. what are you trying to do?