Python-instantiating an object in a loop with independent processing

I have a simple electoral program. Below are the requirements:

  • class Politician

  • Randomized voices.
  • Taking the number of politicians as input from the user.

    num_politicians = input("The number of politicians: ")
    
          

  • Quoting and instantiation

    names = []
    for x in range(num_politicians):
        new_name = input("Name: ")
        while new_name in names:
            new_name = input("Please enter another name: ")
        names.append(new_name)
    
        #### This part is the crux of my problem
        ### Create instances of the Politician class
        #### I want to do this in a way so that i can independently 
        #### handle each instance when i randomize and assign votes
    
          

I watched:

However, I could not find a solution to my problem

The class "Politician" is below:

class Politician:

    def __init__(self, name):
        self.name = str(name)
        self.age = age
        self.votes = 0

    def change(self):
        self.votes = self.votes + 1

    def __str__(self):
        return self.name + ": " + str(self.votes)

      

Desired output:

>>> The Number of politicians: 3
>>> Name: John
>>> Name: Joseph
>>> Name: Mary
>>> Processing...
(I use time.sleep(1.0) here)
>>> Mary: 8 votes
>>> John: 2 votes
>>> Joseph: 1 vote

      

My problem is one statement

I want to instantiate an instance in a for-loop in such a way that I can arbitrarily assign them to votes (this, I suppose, requires me to handle instances independently.)

Any help would be appreciated.

+3


source to share


1 answer


You can keep your instances in a list:

politicians = []
for name in 'ABC':
    politicians.append(Politician(name))

      

You can now access individual instances:

>>> politicians[0].name
'A'

      

I used a modified version of your class that gives each policy a default age if not:

class Politician:

    def __init__(self, name, age=45):
        self.name = str(name)
        self.age = age
        self.votes = 0

    def change(self):
        self.votes = self.votes + 1

    def __str__(self):
        return self.name + ": " + str(self.votes)

      

Now you can work with the list of politicians:

print('The Number of politicians: {}'.format(len(politicians)))

      

prints:

The Number of politicians: 3

      

for politician in politicians:
    print(politician)

      

prints:

A: 0
B: 0
C: 0

      

Assign random votes:



import random

for x in range(100):
    pol = random.choice(politicians)
    pol.votes += 1

      

Now:

for politician in politicians:
    print(politician)

      

prints:

A: 35
B: 37
C: 28

      

The whole program:

# Assuming Python 3.

class Politician:

    def __init__(self, name, age=45):
        self.name = str(name)
        self.age = age
        self.votes = 0

    def change(self):
        self.votes = self.votes + 1

    def __str__(self):
        return '{}: {} votes'.format(self.name, self.votes)

num_politicians = int(input("The number of politicians: "))
politicians = []
for n in range(num_politicians):
    if n == 0:
        new_name = input("Please enter a name: ")
    else:
        new_name = input("Please enter another name: ")
    politicians.append(Politician(new_name))

print('The Number of politicians: {}'.format(len(politicians)))
for politician in politicians:
    print(politician)

print('Processing ...')
for x in range(100):
    pol = random.choice(politicians)
    pol.votes += 1

for politician in politicians:
    print(politician)

      

And usage:

The number of politicians: 3
Please enter a name: John
Please enter another name: Joseph
Please enter another name: Mary
The Number of politicians: 3
John: 0 votes
Joseph: 0 votes
Mary: 0 votes
Processing ...
John: 25 votes
Joseph: 39 votes
Mary: 36 votes

      

UPDATE

As @martineau points out, for real problems, a dictionary will be more useful.

Create a dictionary instead of a list:

politicians = {}

      

and in the loop we will use the name as a key when adding your instance:

politicians[new_name] = Politician(new_name)

      

+7


source







All Articles