根据用户输入创建和存储一组#个类对象

时间:2018-10-10 18:36:31

标签: python python-3.x user-input

我正在制作一个游戏得分计算器,该计算器需要能够提示用户输入玩家人数,然后询问每个玩家信息,并将其存储在每一轮得分中。

我当前遇到的问题是如何写出一个用户输入提示,该提示创建给定数量的Player对象,然后为每个新创建的对象分配其自己的可调用变量(最好是打印输入的玩家名称)

我尝试使用基于给定数字创建输入循环的输入,但是它只要求输入而没有存储任何内容。

这是我的Player班:

class Player:
    def __init__(self, name, inca, power, power_mask, short, medium, long):
        self.name = name
        self.inca = inca
        self.power = power
        self.power_mask = power_mask
        self.short = short
        self.medium = medium
        self.long = long

最好以某种方式编写一个def来创建一个类对象,该类对象具有我需要将其存储在其中的所有参数吗? 如果是这样,我怎么写出来?

1 个答案:

答案 0 :(得分:0)

也许沿着这些思路,您可以创建一个包含所有实例的字典,然后可以根据需要使用名称来调用它们。

mysql> SET profiling = 1;
mysql> select * from bigtable where id in (200, 900, 22000, 88888);
mysql> select * from bigtable where id =200
-> union all
-> select * from bigtable where id =900
-> union all
-> select * from bigtable where id =22000
-> union all
-> select * from bigtable where id =88888;
mysql> show profile for query 1;
+----------------------+----------+
| Status               | Duration |
+----------------------+----------+
| starting             | 0.000046 |
| checking permissions | 0.000006 |
| Opening tables       | 0.000013 |
| init                 | 0.000024 |
| System lock          | 0.000008 |
| optimizing           | 0.000009 |
| statistics           | 0.000031 |
| preparing            | 0.000010 |
| executing            | 0.000003 |
| Sending data         | 0.000072 |
| end                  | 0.000004 |
| query end            | 0.000006 |
| closing tables       | 0.000006 |
| freeing items        | 0.000013 |
| cleaning up          | 0.000008 |
+----------------------+----------+
15 rows in set, 1 warning (0,00 sec)

mysql> show profile for query 2;
+----------------------+----------+                                                                                                                                 
| Status               | Duration |
+----------------------+----------+
| starting             | 0.000054 |
| checking permissions | 0.000006 |
| checking permissions | 0.000002 |
| checking permissions | 0.000003 |
| checking permissions | 0.000004 |
| Opening tables       | 0.000112 |
| init                 | 0.000058 |
| System lock          | 0.000009 |
| optimizing           | 0.000008 |
| statistics           | 0.000032 |
| preparing            | 0.000009 |
| optimizing           | 0.000005 |
| statistics           | 0.000017 |
| preparing            | 0.000005 |
| optimizing           | 0.000005 |
| statistics           | 0.000014 |
| preparing            | 0.000005 |
| optimizing           | 0.000005 |
| statistics           | 0.000014 |
| preparing            | 0.000006 |
| executing            | 0.000003 |
| Sending data         | 0.000018 |
| executing            | 0.000003 |
| Sending data         | 0.000008 |
| executing            | 0.000003 |
| Sending data         | 0.000007 |
| executing            | 0.000003 |
| Sending data         | 0.000007 |
| end                  | 0.000004 |
| query end            | 0.000007 |
| removing tmp table   | 0.000009 |
| query end            | 0.000002 |
| closing tables       | 0.000008 |
| freeing items        | 0.000020 |
| cleaning up          | 0.000011 |
+----------------------+----------+
35 rows in set, 1 warning (0,00 sec)
class Player:
    def __init__(self, name, inca, power, power_mask, short, medium, _long):
        self.name = name
        self.inca = inca
        self.power = power
        self.power_mask = power_mask
        self.short = short
        self.medium = medium
        self.long = _long

n = int(input('Enter amount of players: '))
d = {}
for i in range(n):
    name = input('Enter name: ')
    inca = input('Enter inca: ')
    power = input('Enter power: ')
    power_mask = input('Enter power mask: ')
    short = input('Enter short: ')
    medium = input('Enter medium: ')
    _long = input('Enter long: ')
    d[name] = Player(name, inca, power, power_mask, short, medium, _long)

print(d['vash'].power)