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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T05:06:12+00:00 2026-06-16T05:06:12+00:00

so I have a question regarding dictionaries in Python. I’d like to create a

  • 0

so I have a question regarding dictionaries in Python.
I’d like to create a dictionary where the user will be prompted with 2 options; either updating the dictionary or clearing the dictionary.
Let me first show you my code:

def myStuff():
    food = {'Apple': 0, 'Banana': 0, 'Grapes': 0}
    choice = raw_input('Please pick an option:\n1) Update number of food I have\n2) Clear all\n>>')
    if choice == str(1):
        apple = int(raw_input('How many apples do you want to add?\n>>'))
        banana = int(raw_input('How many bananas do you want to add?\n>>'))
        grapes = int(raw_input('How many grapes do you want to add?\n>>'))
        print 'Updating...'
        food['Apple'] = apple
        food['Banana'] = banana
        food['Grapes'] = grapes
        print  food
    elif choice == str(2):
        food['Apple'] = 0
        food['Banana'] = 0
        food['Grapes'] = 0
        print food
    else:
        return False

myStuff()

Now here’s what I’d like to add:

1.The ability for the user to keep updating the dictionary (which means that if someone input 10 apples, the dictionary will store 10 apples and the user will be prompted again to input the number of apples he want to input to update the dictionary).
I’m not really sure how to implement a loop into this.

2, The ability for the user to clear his dictionary after updating it. For example: if someone inputs 10 apples, the loop will ask the user again if he/she would like to clear the dictionary.

It’s kind of like a bank where someone deposits money and clears their account if they don’t have any money left in there account.

  • 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-16T05:06:13+00:00Added an answer on June 16, 2026 at 5:06 am

    With what you are doing, it might be better to create your own class. You should also have different functions for all of the different actions you wish to do. ie. Update, Clear, Add

    class Fruit:
        Apple = 0
        Banana = 0
        Grape = 0
    
        def __repr__(self):
             return "You have {self.Apple} apples, {self.Banana} bananas, and {self.Grape} grapes".format(**locals())
    
        def update(self):
           while 1:
             choice = raw_input('Please pick an option:\n1) Update number of food I have\n2) Clear all\n>>')
             if (int(choice) == 1):
                self.Add()
             elif (int(choice) == 2):
                self.Clear()
             else:
                 print "Input not valid"
    
        def Add(self):
           self.Apple += int(raw_input('How many apples do you want to add?\n>>'))
           self.Banana += int(raw_input('How many bananas do you want to add?\n>>'))
           self.Grape += int(raw_input('How many grapes do you want to add?\n>>'))
           print self
    
        def Clear(self):
           self.Apple = 0
           self.Banana = 0
           self.Grape = 0
           print self
    
    if __name__ == "__main__":
        fruit = Fruit()
        fruit.update()         
    

    You should also look in to using try and except statements to make sure the program does not crash if the wrong input is used. Also you should add in an exit command to exit the program, otherwise this will loop forever. for example. if the user input is “exit” then have a conditional notice it and break.

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

Sidebar

Related Questions

I have a fairly basic question regarding python lists/dictionaries which I would like some
I have a question regarding django templates and html frameset's. I would like to
I have a question regarding interfaces. I understood that you cannot create instances of
I have a question regarding template parts. I want to create a control that
I have a question regarding left join on SQL: I would like to know
I have a question regarding vectors: If I have a std::vector<MyClass> will this vector
I have a question regarding inheritance, so I will describe the scenario below: I
I have a question regarding an update function I created... CREATE OR REPLACE FUNCTION
I have question regarding the SQLAlchemy. How can I add into my mapped class
I have question regarding the use of function parameters. In the past I have

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.