Below is a compact, beginner-friendly “Apples & Oranges Counter” in Python.
Copy-paste it into a file called (for example) fruit_counter.py and run with python fruit_counter.py.
It repeatedly asks the user for a fruit name until they type q (or quit).
At the end it prints how many apples, oranges, and “other” fruits were entered.
#!/usr/bin/env python3
"""
Apples & Oranges Counter
------------------------
Interactive CLI script that keeps track of how many apples and oranges
a user types in. Any other fruit name is lumped into an “other” bucket.
"""
from collections import Counter
def main() -> None:
print("🍏 Welcome to the Apples & Oranges Counter! 🍊")
print("Type a fruit name each time (apple / orange / anything).")
print("Type 'q' or 'quit' to finish.\n")
counts = Counter() # empty counter
while True:
fruit = input("Enter fruit (or q to quit): ").strip().lower()
if fruit in {"q", "quit"}: # graceful exit
break
elif fruit in {"apple", "apples"}:
counts["apple"] += 1
print(f" -> Apple count: {counts['apple']}")
elif fruit in {"orange", "oranges"}:
counts["orange"] += 1
print(f" -> Orange count: {counts['orange']}")
else:
counts["other"] += 1
print(f" -> Other fruits: {counts['other']}")
# -------------------- Summary --------------------
print("\n=== Final tally ===")
print(f"Apples : {counts['apple']}")
print(f"Oranges: {counts['orange']}")
print(f"Other : {counts['other']}")
print("\nThanks for counting fruits! 🍎🍊")
if __name__ == "__main__":
main()
How it works (quick breakdown)
- We import
Counter from collections – that’s an easy way to store frequency counts.
- We loop forever (
while True) prompting the user.
- Any string in the stop set
{"q", "quit"} breaks the loop.
- Depending on the entry, we increment
counts["apple"], counts["orange"], or counts["other"].
- At exit we simply print the stored counters.
Extras / mini-challenges
• Add persistence: write the counts to a JSON file so the next run resumes where you left off.
• Support plurals & misspellings with the fuzzywuzzy or rapidfuzz library.
• Replace the CLI with a tiny Tkinter GUI (entry box + labels).
Happy coding!