Click here to Skip to main content
15,895,084 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to guess a date in ten questions. I can get the code to ask only ten questions, but I am having trouble getting to the correct date. The program keeps going down to Decemeber 31 and I need it to narrow down to a specific date. I am still very new to programming so any help would be great.

What I have tried:

Python
#Guess the Date
#Instructions for program
print ('Think of a specific date in any year')
print ('e.g., Jan 1 or Feb 29 or Jul 4 or Dec 25')
print ('Truthfully answer "Yes" or "No" to the following questions')
print ('I will determine the date in ten questions or less')

#Return a list of elements, each element is a date in a calendar year
def Calendar(monthNames, numDaysInMonth): #defining Calendar

    if len(monthNames) != len(numDaysInMonth): 
        return []

    dates = []
    idx = 0     #index is set to zero

    while idx < len(monthNames):
        for date in range(1, numDaysInMonth[idx] + 1):
            dates.append(monthNames[idx] + " " + str(date))
        idx = idx + 1
    return dates
monthNames = ["Jan", "Feb", "Mar", "Apr", "May", "Jun",
          "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]   #list of months
numDaysInMonth = [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31] #list of how many days in each month

#This is a binary search
first =Calendar(monthNames,numDaysInMonth) #first defined through the Calendar code block

def guess_game(first = Calendar(monthNames,numDaysInMonth)): #defining guess_game using months list and numDays item in the Calendar code to work the search

    if len(first) == 1:
        return first[0]

    mid = len(first)//2

    if is_earlier(first[mid]):    #list mindpoint
        return guess_game(first[:mid])
    else:
        return guess_game(first[mid:])

#Answer output, what is out putted in the python shell
def is_earlier(guess = 10): #defining is_ealier and setting the number of guesses equal to 10

    answer = input("Is {} earlier then your date? (Yes - earlier /No - not earlier) ".format(guess))#10 or less guesses to to find answer
    
    if answer.upper() == "Yes": #if true user can put No, no, n
        return True

    else:
        return False #if false user can put Yes, yes, y
        
guess_game() #intialize quess_game
Posted
Updated 5-Aug-17 14:18pm
v2

1 solution

Your test of answer is wrong.
What happen to user answer when you do answer.upper() ?
You are testing against what value ?

There is a tool that allow you to see what your code is doing, its name is debugger. It is also a great learning tool because it show you reality and you can see which expectation match reality.
When you don't understand what your code is doing or why it does what it does, the answer is debugger.
Use the debugger to see what your code is doing. Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]
27.3. pdb — The Python Debugger — Python 3.6.1 documentation[^]
Debugging in Python | Python Conquers The Universe[^]
pdb – Interactive Debugger - Python Module of the Week[^]
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.
 
Share this answer
 
Comments
Member 13342931 5-Aug-17 20:28pm    
Thank you for the information about the debugger and for letting me know my answer is the problem.
Patrice T 5-Aug-17 20:30pm    
You're welcome.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900