i know nothing about programming but started learning python yesterday, and have learned some basic things. my dad is a programmer and challenged me to write a program that would be like '20 Questions'. i started it using the few things i know, and i think the way i started would work, but i also think there is other better ways to do it. any ideas?

here is what i wrote. the finished 'game' would be really limited and would limit the users choice of word to maybe 30 different words.

# sectioned off to person place or thing
# 9 other q's for each path

# layout: 
# question1>option - yes>question2A>option - yes>question3A
#                                          - no>question3B

#                  - no>question2B>option - yes>question3C
#                                         - no>question3D             


print(All the questions will require you to type either 'yes' or 'no'.")
import time
time.sleep(2)

1a = input("Are you ready? ")

if 1a == "yes":
  print("Let's get started! First question:")
  b = input("? ")

  if b == "yes":
    2a = input("Is it a thing? ")

    if 2a == "yes":
      3a = input("Can it fit in a backpack? ")

    if 2a == "no":
      3b = input("Is it bigger than a car? ")
      
      # pattern would continue-keeps 'branching' out

  if b == "no":
    2b = input("Is it a place? ")
    
    if 2b == "yes":
      3c = input("Can it fit in a backpack? ")

    if 2b == "no":
      3d = input("Is it bigger than a car? ")
      
      # pattern would continue-keeps 'branching' out

if 1a == "no":
  print("Ok. Take your time:)")
  time.sleep(3)
  repeat  

else:
  print("Invalid response. Remember not to use capital letters.")
  time. sleep(3)
  repeat

Edited 5 Years Ago by jpaulsen: n/a

Maybe try something a little simpler. Limit yourself to things inside your house. Make a list of 100 or so items. Then figure out which questions lead to which objects. If every question has a 'y' or 'n' response, what you want is a sequence of y and n results that uniquely identify each object. Make up a dictionary of "Known Responses", keyed by the answer history (the previous responses). Here's a really simple example:

resp = {"",":Is it bigger than a breadbox", # First question, since no history
"y":"Can it be moved", # If bigger than a breadbox ...
"n":"Is it something normally kept in the kitchen", # Not big. Kitchen?
"ny":"Is it some kind of appliance", # Kitchen ... Appliance?
"nyy":"Is it the toaster", # Appliance. Toaster?
"nyn":"Is it a utensil of some sort", # Not appliance. Utensil?
... etc.)

So when the user answers y or n, you tack that on to your answer history and ask question resp[history]. The actual code is a pretty small loop since everything is encoded in the resp[] dictionary. Usually it is better to have "data-driven" programs like this, instead of exploding code.

You'll need to figure out when to stop, and how to handle sequences of answers not covered by the dictionary, but neither is that difficult.

If somebody comes up with an answer you didn't prepare for (say, a pencil) you'll want to ask them to name the item and save that away in a data file. Every couple days you look at things you missed and add them to the dictionary, along with questions to distinguish them from other items.

This is a linked list type of problem, where every question is linked to another for a yes or no response. Each question is assigned a unique number. It does not matter what the number is, it is just a unique identifier for each question. So you can have multiple questions linked to "bigger than a backpack" for example. A simple snippet follows. At some point you will have to guess, so you might want to also be able to input "guess" at any point, and guess in some way.

##         layout =  question        yes  no
question_dic = {1: ("Is it a thing? ", 21, 11), 
                11: ("Is it a place? ", 21, 12),
                12: ("Is it a person? ", 21, 21),
                21: ("Can it fit in a backpack? ", 0, 22),
                22 ("Is it bigger than a car? ", 23, 24),
                23: ("Is it bigger than a house? ", 0, 0),
                24: ("Is it bigger than a table? ", 0, 0)}

store_responses = {}
num = 1
while num != 0:
    question = question_dic[num][0]
    x = ""
    lit = ""
    while x not in ("yes", "no"):
        print lit
        x = raw_input(question).lower()
        lit = "Enter 'Yes' or 'No' only"

    store_responses[num]= x              ## question number & yes or no
    if x == "yes":
        num = question_dic[response][1]
    else:   
        num = question_dic[response][2]

print "Game Over"

Edited 5 Years Ago by woooee: n/a

This question has already been answered. Start a new discussion instead.