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’m not really sure how to start this though. Do I import random? Thank you for your time :)

Answered by ch1ck3n (1591) [earned 5 cycles]
View Answer
Comments
hotnewtop
ch1ck3n (1591)

But you should really try to figure it out yourself by googling

import random

input("Ask me a question >")
print(random.choice(["yes", "no", "maybe"]))
JULIODIAZ1 (46)

you could do random and you can have easter eggs on some phrases that have special responses and hid ur code somehow o people dont just cheat and look in the code...idk

JULIODIAZ1 (46)

np :)
question, what laguage will you be doing this in?

@SimplyMiah

IMayBeMe (336)

I don't want to give you all the code straight away but I'll walk you through the process to do this.

So to start, you want to make an array or list as it's known in python containing all the possible replies. This could be done like this:

your_list = ['first_response', 'second_response',]

You can add as many string responses as you want in the list by separating them with commas.

Then you can ask the user to input a question by using input('your prompt here'). The input itself is completely irrelevant and can be forgotten about. To reply to the question asked you need to pick a random response and print() it using the random module. To do this you would write:

import random
print(random.choice(your_list_variable_name_here))

This does exactly what it looks like and randomly chooses an element from an list and prints it out.

Also here is a resource to help with the random module: https://www.w3schools.com/python/module_random.asp