-2

how would I be able to make something like this that actually works?

choices = input('Enter choices. ')

i = 0
while i < 10:
    number_of_{}s.format(i) = choices.count(str([i]))
    i += 1
  • For a normal case, use a `dict` or `list`. For this case, [`collections.Counter`](https://docs.python.org/2/library/collections.html#collections.Counter). – Two-Bit Alchemist Jun 02 '16 at 22:31
  • That depends on what you mean by "actually works". Clarify, please. Check the posting guidelines for ... well, guidelines. :-) – Prune Jun 02 '16 at 22:31
  • If you're trying to create a bunch of related variables, we give them a single name and use a sequence (tuple, list) or collection (set, dictionary) of some sort. – Prune Jun 02 '16 at 22:32

1 Answers1

1

Use the tools the language gave you for this problem. Use lists.

i = 0
number_of = []
while i < 10:
    number_of.append(choices.count(str([i])))
    i += 1

Even better, use for.

number_of = []
for i in range(10): number_of.append(choices.count(str([i])))

Or, best, a list comprehension.

number_of = [choices.count(str([i])) for i in range(10)]
John Kugelman
  • 349,597
  • 67
  • 533
  • 578
Itay Moav -Malimovka
  • 52,579
  • 61
  • 190
  • 278