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

  • Home
  • SEARCH
  • 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 8860183
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T15:13:07+00:00 2026-06-14T15:13:07+00:00

Challenge is to improve a previous challenge, Who’s Your Daddy (see my successful code:

  • 0

Challenge is to improve a previous challenge, “Who’s Your Daddy” (see my successful code: http://pastebin.com/AU2aRWHk) by adding a choice that let’s the user enter a name and get back a grandfather. Program should still only use one dictionary of son-father pairs.

I cannot get this to work. My entire code so far can be seen at: http://pastebin.com/33KrEMhT

I’ve obviously made this WAY more difficult than it need to be and am now trapped in a world of complexity. Here’s code I’ve F’d up:

# create dictionary
paternal_pairs ={"a": "b",
                 "b": "c",
                 "c": "d"}

# initialize variables
choice = None

# program's user interface
while choice != 0:
print(
"""
   Who's Yo Daddy:

   2 - Look Up Grandfather of a Son
   """
)

choice = input("What would you like to do?: ")
print() 

    # look up grandfather of a son
    if choice == "2":
        son = input("What is the son's name?: ")
        # verify input exists in dictionary
        if son in paternal_pairs.values():
            for dad, boy in paternal_pairs.items():
                if dad == son:
                    temp_son = dad
                    for ol_man, kid in paternal_pairs.items():
                        if temp_son == kid:
                            print("\nThe grandfather of", son, "is", ol_man)
                        else:
                            print("\nNo grandfather listed for", son)
                else:
                    print("\nNo grandfather listed for", son)
        # if input does not exist in dictionary:
        else:
            print("Sorry, that son is not listed. Try adding a father-son pair.")

After choosing “2”, my output:

What is the son's name?: d

No grandfather listed for d

No grandfather listed for d

No grandfather listed for d

No grandfather listed for d

No grandfather listed for d

No grandfather listed for d

No grandfather listed for d

No grandfather listed for d

Obviously temporarily trapped in a small loop and it doesn’t work. All other code works as expected. 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-14T15:13:08+00:00Added an answer on June 14, 2026 at 3:13 pm

    You loop over each entry in the dict, and match the value, and if it doesn’t match, then for each key-value pair you print that it doesn’t match.

    It is the equivalent of the following simplified loop:

    >>> for i in range(3):
    ...     if i == 5:
    ...         print(i)
    ...     else:
    ...         print('Not 5')
    ... 
    Not 5
    Not 5
    Not 5
    

    Use the else: clause of the for loop instead, it’ll only be invoked if you completed going through all the values; use a break if you find a match:

    for ol_man, kid in paternal_pairs.items():
        if temp_son == kid:
            print("\nThe grandfather of", son, "is", ol_man)
            break
    else:
        print("\nNo grandfather listed for", son)
    

    A small demonstration of how the else: clause works when used with a for loop:

    >>> for i in range(3):
    ...     if i == 1:
    ...         print(i)
    ...         break
    ... else:
    ...     print('Through')
    ... 
    1
    >>> for i in range(3):
    ...     if i == 5:
    ...         print(i)
    ...         break
    ... else:
    ...     print('Through')
    ... 
    Through
    

    In the first example, we broke out of the loop with a break, but in the second example we never reached the break statement (i never was equal to 5) so the else: clause is reached and Through is printed.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

What is a good challenge to improve your skills in object oriented programming? The
Challenge: I have this code that fails to compile. Can you figure out what's
The challenge The shortest code by character count to generate a wave from the
The challenge The shortest code by character count, that will output musical notation based
The challenge The shortest code by character count to output a spider web with
The challenge The shortest code by character count that will generate a beehive from
The Challenge Guidelines for code-golf on SO The shortest code by character count to
The challenge The shortest code by character count that will output the numeric equivalent
by keeping inspiration from this http://www.filamentgroup.com/examples/menus/ipod.php i have maked my own from scratch cause
I am trying to solve this challenge on InterviewStreet: https://www.interviewstreet.com/challenges/dashboard/#problem/4edb8abd7cacd I already have a

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.