Python-在具有独立处理的循环中创建对象实例 [英] Python- creating object instances in a loop with independent handling
问题描述
我有一个简单的选举程序。要求如下:
I have a simple election program. The following are the requirements:
-
班级政治家
- 随机投票。
-
以政客人数作为用户输入。
class Politician
- Randomized votes.
Taking number of politicians as input from user.
num_politicians = input("The number of politicians: ")
循环并创建实例
Looping and creating instances
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
我看过:
- How do you create different variable names while in a loop? (Python)
- Python: Create instance of an object in a loop
但是我找不到我的解决方案问题
However I could not find a solution to my problem
政治家课如下:
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)
所需的输出:
>>> 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
我在一个陈述中的问题
我想在for循环中创建类实例,这样我可以为他们随机分配票数(我想这将要求我独立处理实例。)
I want to create class instances in the for-loop in such a way that i can assign them votes randomly (This would, I suppose, require me to independently handle instances.)
任何帮助将不胜感激。
推荐答案
您可以将实例存储在列表:
You can store your instances in a list:
politicians = []
for name in 'ABC':
politicians.append(Politician(name))
现在您可以访问单个实例:
Now you can access individual instances:
>>> politicians[0].name
'A'
我使用了您课程的修改版本如果没有提供,则为每个政治家提供默认年龄:
I used a modified version of your class that gives each politician a default age if no is provided:
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 your list of politicians:
print('The Number of politicians: {}'.format(len(politicians)))
张照片:
The Number of politicians: 3
此
for politician in politicians:
print(politician)
印刷品:
A: 0
B: 0
C: 0
分配随机投票:
import random
for x in range(100):
pol = random.choice(politicians)
pol.votes += 1
现在:
for politician in politicians:
print(politician)
打印:
A: 35
B: 37
C: 28
整个程序:
# 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)
用法:
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
更新
如@martineau所建议的现实生活中的问题,字典将对
更为有用。
UPDATE
As @martineau suggests, for real-live problems a dictionary would be more useful.
创建字典而不是列表:
politicians = {}
,然后在循环中添加实例时,将名称作为关键字:
and in the loop us the name as key when you add your instance:
politicians[new_name] = Politician(new_name)
这篇关于Python-在具有独立处理的循环中创建对象实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!