Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8844607
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T11:31:48+00:00 2026-06-14T11:31:48+00:00

I am teaching myself Python and have written a small script for exchanging Christmas

  • 0

I am teaching myself Python and have written a small script for exchanging Christmas gifts (this is not homework). My family likes for each person to give one gift to one person of the same gender. The following script works most of the time but sometimes fails through infinite recursion. I am not sure why, because I think the base case will eventually be met.

import random

family = {'Joe': 'm', 'Jane': 'f', 'John': 'm', 'Jill': 'f', 'James': 'm', 'Jade': 'f'}
receivers = family.copy()
givers = family.copy()

def match(giver):
    index = random.randrange(len(receivers))
    giverGender = givers[giver]
    receiver = receivers.keys()[index]
    receiverGender = receivers.values()[index]

    if giver != receiver and giverGender == receiverGender:
        del receivers[receiver]
        return giver + ' to ' + receiver
    else:
        return match(giver)

# main program
for i in givers:
    print match(i)

This is the error (EDIT to add full error):

Traceback (most recent call last):
  File "C:\...\christmasGifts.py", line 22, in <module>
    print match(i)
  File "C:\...\christmasGifts.py", line 18, in match
    return match(giver)

  ...

  File "C:\...\christmasGifts.py", line 18, in match
    return match(giver)
  File "C:\...\christmasGifts.py", line 9, in match
  index = random.randrange(len(receivers))
  File "C:\Python27\lib\random.py", line 184, in randrange
istart = int(start)
RuntimeError: maximum recursion depth exceeded while calling a Python object

Thank you for any help.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T11:31:49+00:00Added an answer on June 14, 2026 at 11:31 am

    First lets just consider the women. If your program runs and matches Jane to Jill, then matches Jill to Jane, Jane is the only female left and because she cannot match herself, your program runs indefinitely without a match.

    Let me propose an alternate method to solve your problem. Randomly shuffle the order of your givers/recieves and have each person give a gift the the following person in the list, and have the last person in the list gift the first person. That would look something like this:

    from random import shuffle
    women = ['Jane', 'Jill', 'Jade']
    shuffle(women)
    print women[-1] + ' to ' + women[0]
    for i in range(len(women) - 1):
        print women[i] + ' to ' + women[i+1]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

As part of teaching myself python I've written a script which allows a user
I am teaching myself python. I was thinking of small programs, and came up
I'm teaching myself Python and I was translating some sample code into this class
I've been teaching myself C++ and someone told me that C++ does not have
I am teaching myself some Python and I have come across a problem which
I'm teaching myself Python and my most recent lesson was that Python is not
I am self teaching myself some python OOP, and I have created a simple
I am teaching myself python from this site . On Chapter 3 , when
I have been teaching myself OpenGL programming using the Python wrapper PyOpenGL, and am
I am noob with python, been on and off teaching myself since this summer.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.