0

我试图让 first_name 和 last_name 组合在一起成为一个新的字典条目。多少取决于有多少人在商店购买。如果我购买 50 名士兵,我得到的总是少于那个。

import random, math
first_name = [ "Emily", "Steve" , "Dave" , "Bob" , "James" , "Jim" , "Jenny" , "Will" , "Ryan" ]
last_name = [ "Wright" , "Kalman" , "Meitzen" , "Cole" , "Robins" , "Harrison" , "Saturn" ]
troops = {}
money = 1000

def shop():
    loop = 0
    global money, first_name, last_name, troops
    while loop == 0 :
        print("""
        Your Money = {}
        (Number , Item , cost)
        0, Quit ,N/A
        1, Troop, 10
        """.format(money))
        shopq = int( input("What do you want to buy"))

        if shopq == 1:
            shopq2 = int( input("How many"))
            if shopq2 > money :
                print(" You cannot by this many")
            else:
                print("You can buy that many")
                money = money - shopq2
                troop_number = 0
                while troop_number < shopq2 :
                    s_name = random.choice(first_name) + " " + random.choice(last_name)
                    troops[s_name] = 100
                    troop_number = troop_number + 1
                print(troops)

            print(" Money = {}".format(money))
        elif shopq == 0:
            break

class dropship:
    def create(self, troops):
        troop_number = 0
        for k in troops :
            troop_number = troop_number + 1
        print("troops = {}".format(troop_number))

shop()
x = dropship()
x.create(troops)

输出:

        Your Money = 1000
        (Number , Item , cost)
        0, Quit ,N/A
        1, Troop, 10

What do you want to buy1
How many50
You can buy that many
{'Ryan Wright': 100, 'Bob Cole': 100, 'Bob Kalman': 100, 'Will Wright': 100, 'Dave Cole': 100, 'Dave Robins': 100, 'Emily Kalman': 100, 'Jenny Kalman': 100, 'Bob Harrison': 100, 'Emily Wright': 100, 'Will Cole': 100, 'Jim Wright': 100, 'Dave Kalman': 100, 'Dave Wright': 100, 'Bob Meitzen': 100, 'Jenny Wright': 100, 'Jenny Harrison': 100, 'Dave Saturn': 100, 'James Robins': 100, 'Bob Robins': 100, 'Dave Meitzen': 100, 'Steve Wright': 100, 'Bob Wright': 100, 'Steve Kalman': 100, 'Ryan Harrison': 100, 'Jim Saturn': 100, 'Steve Robins': 100, 'Ryan Cole': 100, 'Jim Meitzen': 100, 'James Cole': 100, 'Emily Cole': 100, 'Ryan Saturn': 100, 'Steve Harrison': 100}
 Money = 950

        Your Money = 950
        (Number , Item , cost)
        0, Quit ,N/A
        1, Troop, 10

What do you want to buy0
troops = 33
4

2 回答 2

4

您正在创建随机名称,其中一些随机名称将是相同的,因此它们会替换字典中的先前条目(字典键是唯一的)。你必须改变你这样做的方式。例如:

import random
import itertools
random.sample(list(itertools.product(first_name, last_name)), 50)

但是你也应该得到更大的名字和姓氏池,否则你只能有 63 个不同的全名。

于 2013-09-07T15:27:14.063 回答
1

您的字典的问题是字典键必须是唯一的。由于您使用随机选择的名称拼接在一起作为键,您很可能会生成'Ryan Wright'(例如)不止一次。

这是您的代码正在执行的操作,导致您得出“短”计数:

troops['Ryan Wright'] = 100
troops['Bob Cole'] = 100
troops['Ryan Wright'] = 100

第三个分配使用字典中的相同槽,troops因为键是相同的。如果您的代码只是那三行,那么您将拥有一个包含两个条目的字典,而不是您希望的三个。您可以通过添加以下assert语句在代码中看到这种情况:

s_name = random.choice(first_name) + " " + random.choice(last_name)
assert s_name not in troops
troops[s_name] = 100

它不会解决您的问题,但会告诉您您的键正在碰撞。

于 2013-09-07T15:29:42.500 回答