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 8854811
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T13:58:12+00:00 2026-06-14T13:58:12+00:00

This is my first post and I’m quite new at programming, so I might

  • 0

This is my first post and I’m quite new at programming, so I might not be able to convey my question appropriately, but I’ll do my best!

tries_dict = {1:'first', 2:'second', 3:'third', 4:'fourth', ub_tries:'last'}

ub_tries = user input

tries = 1

input ('\nCome on make your ' + tries_dict.get(tries) + guess: ')

These 3 elements are part of a number guess game I created, and I included them in a while loop where tries += 1 after each wrong answer.

As you can see, in my dictionary there are custom values for the first 4 answers and the last possible chance before the game is over, so here is what I tried to do:

I wanted to find a way to have the ‘NEXT’ value for every answer/key between ‘fourth’ and ‘last’.

As in:

tries = 5

Come on make your next guess

tries = 6

Come on make your next guess

and so on

I did find a way with some complex looping, but being the curious type I wanted to know of more efficient/practical ways to accomplish this.

Here are some options i thought about but couldn’t get to work:

  1. Using a range as a key
  2. Finding a way to generate a list with values between 4 and ub_tries and using that list as a key

So generally speaking: how can one create a way to have this general answer (next or whatever) for keys that aren’t specified in a dictionary?

Any feedback would be greatly appreciated, feel free to ask for clarifications since I can tell myself my question is kind of messy.

I hope I get more crafty both at programming and asking related questions, so far my programming is nearly as messy as my summary skills, sigh!

  • 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-14T13:58:14+00:00Added an answer on June 14, 2026 at 1:58 pm

    I’m not sure whether this is what you want, but dict.get may be the answer:

    >>> ub_tries = 20
    >>> tries_dict = {1:'first', 2:'second', 3:'third', 4:'fourth', ub_tries:'last'}
    >>> tries_dict.get(1, 'next')
    'first'
    >>> tries_dict.get(4, 'next')
    'fourth'
    >>> tries_dict.get(5, 'next')
    'next'
    >>> tries_dict.get(20, 'next')
    'last'
    >>> tries_dict.get(21, 'next')
    'next'
    

    Of course you could wrap this up in a function, in various different ways. For example:

    def name_try(try_number, ub_tries):
        tries_dict = {1:'first', 2:'second', 3:'third', 4:'fourth', ub_tries:'last'}
        return tries_dict.get(try_number, 'next')
    

    At any rate, dict.get(key, default=None) is like dict[key], except that if key is not a member, instead of raising a KeyError, it returns default.

    As for your suggestions:

    using a range as a key??

    Sure, you can do that (if you’re in Python 2 instead of 3, use xrange for range), but how would it help?

    d = { range(1, 5): '???', 
          range(5, ub_tries): 'next', 
          range(ub_tries, ub_tries + 1): 'last' }
    

    That’s perfectly legal—but d[6] is going to raise a KeyError, because 6 isn’t the same thing as range(5, ub_tries).

    If you want this to work, you could build a RangeDictionary like this:

    class RangeDictionary(dict):
        def __getitem__(self, key):
            for r in self.keys():
                if key in r:
                    return super().__getitem__(r)
            return super().__getitem__(key)
    

    But that’s well beyond “beginners’ Python”, even for this horribly inefficient, incomplete, and non-robust implementation, so I wouldn’t suggest it.

    finding a way to generate a list with values between 4 and ub_tries and using such list as a key

    You mean like this?

    >>> ub_tries = 8
    >>> tries_dict = {1:'first', 2:'second', 3:'third', 4:'fourth', ub_tries:'last'}
    >>> tries_dict.update({i: 'next' for i in range(5, ub_tries)})
    >>> tries_dict
    {1: 'first', 2: 'second', 3: 'third', 4: 'fourth', 5: 'next', 6: 'next', 7: 'next', 8: 'last'}
    >>> tries_dict[6]
    'next'
    

    That works, but it’s probably not as good a solution.

    Finally, you could use defaultdict, which lets you bake the default value into the dictionary, instead of passing it as part of each call:

    >>> from collections import defaultdict
    >>> tries_dict = defaultdict(lambda: 'next', 
    ...                          {1:'first', 2:'second', 3:'third', 4:'fourth', ub_tries:'last'})
    >>> tries_dict
    defaultdict(<function <lambda> at 0x10272fef0>, {8: 'last', 1: 'first', 2: 'second', 3: 'third', 4: 'fourth'})
    >>> tries_dict[5]
    'next'
    >>> tries_dict
    defaultdict(<function <lambda> at 0x10272fef0>, {1: 'first', 2: 'second', 3: 'third', 4: 'fourth', 5: 'next', 8: 'last'})
    

    However, note that this permanently creates each element the first time you ask for it—and you have to create a function that returns the default value. This makes it more useful for cases where you’re going to be updating values, and just want a default as a starting point.

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

Sidebar

Related Questions

this is my first post here. I'm new in Android Programming. I want to
This is my first post, I'm new to this site, but I've been lurking
This is my first post on stackoverflow, so please excuse me if my question
This is my first post, thx, you've been very helpful. But I'm stuck. I
this is my first post on this website, but I'm all the time getting
This is my first post, so please forgive me if this question has been
this is my first post here. I'm using an existing Flash widget but would
This is my first post on stackoverflow, I hope one of many! My question
Hey all, first post and a noob in Android programming, but willing to learn!
this is my first post here. First off, some background, I'm new to C#

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.