小编Aka*_*chi的帖子

Python-在具有独立处理的循环中创建对象实例

我有一个简单的选举计划.以下是要求:

  1. class Politician
  2. 随机投票.
  3. 以政治家的数量作为用户的投入.

    num_politicians = input("The number of politicians: ")
    
    Run Code Online (Sandbox Code Playgroud)
  4. 循环和创建实例

    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
    
    Run Code Online (Sandbox Code Playgroud)

我看过:

  1. 如何在循环中创建不同的变量名?(蟒蛇)
  2. Python:在循环中创建对象的实例

但是我无法找到解决问题的方法 …

python class instances

4
推荐指数
1
解决办法
2万
查看次数

标签 统计

class ×1

instances ×1

python ×1