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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T23:21:54+00:00 2026-05-22T23:21:54+00:00

a = 0 b = 0 c = 0 d = 0 fruit =

  • 0
 a = 0
 b = 0
 c = 0
 d = 0

fruit = {
'lemons': [],
'apples': [],
'cherries': [],
'oranges': [],
 }


def count():
    fruit = input("What fruit are you getting at the store? ")
    if fruit == 'lemons':
        fruit['lemons'] = a + 1
    elif fruit == 'apples':
                fruit['apples'] = b + 1
    elif fruit == 'cherries':
                fruit['cherries'] = c + 1

    elif fruit == 'oranges':
                fruit['oranges'] = d + 1
    else: ????

Hey, I’m trying to do two things here: 1) count how many occurrences of a certain word (in this case, certain types of fruit), appear in a document- which I am attempting to simulate here with a simple input function. I know it’s not perfect, but I can’t figure out how to make each occurrence increase the value for the appropriate key incrementally. For instance, if I call this function twice and type “lemons”, the count should be 2, but it remains 1. In other words, my function is a lemon, but I don’t know why.

The last thing I am having trouble with is the else function. 2 ) My program will be looking in pre-defined sections of a document, and I would like my else function to create a key: value pair in the dictionary if the existing key does not exist. For instance, if my program encounters the word ‘banana’, I would like to add the k:v pair { ‘banana’: [] } to the current dictionary so I can start counting those occurrences. But it seems like this would require me to not only add the k:v pair to the dictionary (which I don’t rightly know how to do), but to add a function and variable to count the occurrences like the other k:v pairs.

Does this entire set up make sense for what I’m trying to do? Please 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-05-22T23:21:55+00:00Added an answer on May 22, 2026 at 11:21 pm

    You seem to have multiple variables called fruit, that’s a bad idea. And if you’re just counting, you should start with 0, not []. You can write your code way easier as:

    import collections
    result = collections.defaultdict(int)
    def count():
        fruit = input("What fruit are you getting at the store? ")
        result[fruit] += 1
    

    In Python 3.1+, you should use collections.Counter instead of collections.defaultdict(int). If you don’t want to use the collections module at all, you could also write out the defaultdict functionality:

    result = {}
    def count():
        fruit = input("What fruit are you getting at the store? ")
        if fruit not in result:
            result[fruit] = 0 # Create a new entry in the dictionary. 0 == int()
        result[fruit] += 1
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say I have an array as follows: $food['fruit'] = array('apples','oranges','bananas'); $food['veg'] = array('potatoes','onions','peppers');
Public Enum Fruit Red_Apple = 1 Oranges Ripe_Banana End Enum Private Sub InitCombosRegular() Dim
Here is the table ID WHO FRUIT 1 Adam Apple 2 Adam Lemon 3
fruit = [apple,red,banana,yellow] => [apple, red, banana, yellow] Hash[*fruit] => {apple=>red, banana=>yellow} Why does
enum Fruit { Banana, Orange, Strawberry ... ... // etc, very long enum }
public class Fruit { // choose one public int Id { get; set; }
A B C 1 Fruit 0.34 Pear 2 Vegetable 0.62 Lettuce 3 Fruit 0.80
Is this the best way to select the Fruit object I need from the
I am currently trying to use a bit flag enum in a VB6 /
var fruit:Array = new Array(); var frName:String; var i:Number; save_btn.addEventListener(MouseEvent.CLICK, storeName); function storeName(Event:MouseEvent) {

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.