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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T01:01:45+00:00 2026-06-02T01:01:45+00:00

So I have a Python assignment where I need to use a dictionary to

  • 0

So I have a Python assignment where I need to use a dictionary to hold the items of a menu, then randomly re-key and re-order the menu so that I can change the order of the menu options printed out. Here is an example of the code I have:

ChoiceOne = "Rekey Menu"
ChoiceTwo = "Quit"
menu = {'1':ChoiceOne, '2':ChoiceTwo,}
userChoice = input("Please Enter the Integer for Your Menu Choice: ")

while userChoice not in menu.keys():
    print("Invalid Input")
    userChoice = input("Please Enter the Integer for Your Menu Choice: ")

if menu[userChoice] == ChoiceOne:
    menu = rekey(menu)
elif menu[userChoice] == ChoiceTwo:
    quit()

The above code loops while the user chooses not to quit, repeatedly printing out the menu over and over again. The following is my rekey() function

def rekey(menu):
    rekey = {}
    keylist = random.sample(range(10), 2)
    for i, item in zip(keylist, menu ):
        rekey[i] = menu[item]
    return rekey

My problem seems to present itself in the line where I check whether the user input for the menu option is valid or not. Initially, entering anything other than “1” or “2” will cause the program to enter the While loop until a valid input is entered. After rekeying the menu however, the line “while userChoice not in menu.keys()” is always triggered and no input is ever matched to continue the program.

I’ve tried to find the problem by printing out the new keys in the dictionary and checking to see if the userChoice matches any one of them, but even when I choose a valid new key, the program seems to think that nothing I enter is a valid key.

I hope I’ve described the problem well enough to be understood, thanks for the help in advance!!

  • 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-02T01:01:47+00:00Added an answer on June 2, 2026 at 1:01 am

    In your first case the menu keys are strings, after the rekey they are ints. You need to do a conversion of either the keys or the user input so they match.

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

Sidebar

Related Questions

Suppose I need to create my own small DSL that would use Python to
So I have this code in python that writes some values to a Dictionary
In Python, I have a Graph class that has a dictionary of vertex objects.
I have been asked to write a program using python for an assignment. I
As part of the last assignment in a beginner python programing class, I have
I have Python nested list that I'm trying to organize and eventually count number
i have an issue i could use some help with, i have python list
I have a homework assignment for a Python class and am running into an
I know that variable assignment in python is in fact a binding / re-bindign
Working in Python. I have a function that reads from a queue and creates

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.