<p>您无缘无故地重复定义<code>colors</code>。将它移到脚本的顶部并使其成为常量</p>
<pre><code>COLORS = "Blue Pink Plum Aqua Navy Grey Rose Ruby Teal Gold Jade Lime".split()
</code></pre>
<p>您正在独立生成票证;两次生成相同的值是不太可能的,但也是可能的,而且赔率随票数的增加而线性增加。如果生成一万张票,至少有一张重复的概率约为4%;2万张,约15%;十万张票的价格超过98%。取决于你的用途,也许你不在乎,但这是要记住的(你对两个大奖有什么感觉?)</p>
<p>根据一个人通常买多少张票,你可以把电子邮件放在一个单独的表中,从而节省一些空间。存储一个整数也可以节省不少空间</p>
<pre><code>BASE = 10 ** 8
NUM_COLORS = len(COLORS)
ticket = random.randrange(NUM_COLORS * BASE)
</code></pre>
<p>只是为了展示而分开,比如</p>
<pre><code>color_index, rem = divmod(ticket, BASE)
color = COLORS[color_index]
num_a, num_b = divmod(rem, 10 ** 4)
print("Your ticket is: {} {:04d} {:04d}".format(color, num_a, num_b))
</code></pre>
<p>结果是</p>
<pre><code>Your ticket is: Lime 2592 1700
Your ticket is: Navy 0828 6111
Your ticket is: Lime 3741 7599
Your ticket is: Ruby 4017 4645
Your ticket is: Aqua 0556 1852
Your ticket is: Grey 2486 5298
Your ticket is: Gold 0195 8990
Your ticket is: Navy 9287 8727
Your ticket is: Blue 3736 3443
Your ticket is: Lime 9365 1980
Your ticket is: Plum 2247 9671
Your ticket is: Lime 6568 5285
Your ticket is: Pink 7591 3894
Your ticket is: Grey 6839 4780
Your ticket is: Pink 9348 9882
Your ticket is: Plum 3868 6449
Your ticket is: Rose 2588 7999
Your ticket is: Grey 0625 5061
Your ticket is: Rose 2132 8136
Your ticket is: Navy 0526 4325
</code></pre>