import random
import time
print ' Welcome to the lottery number generator'
num_1 = random.randrange(53)+1 #Random numbers
num_2 = random.randrange(53)+1 #Random numbers
num_3 = random.randrange(53)+1 #Random numbers
num_4 = random.randrange(53)+1 #Random numbers
num_5 = random.randrange(53)+1 #Random numbers
Bonus = random.randrange(42)+1 #Bonus ball random numbers
print 'Here are the numbers that have been drawn'
time.sleep(1)
print 'Your first ball'
time.sleep(1)
print num_1
time.sleep(1)
print 'Your second ball'
time.sleep(1)
print num_2
time.sleep(1)
print 'Your third ball'
time.sleep(1)
print num_3
time.sleep(1)
print 'Your fourth ball'
time.sleep(1)
print num_4
time.sleep(1)
print 'Your fifth ball'
time.sleep(1)
print num_5
time.sleep(1)
print 'The bonus ball'
time.sleep(1)
print Bonus
请有人告诉我如何将随机数放入数字顺序???? 代码工作我只需要将它们按数字顺序排列。 将有5个球需要按数字顺序排列,奖金球需要自己显示。
答案 0 :(得分:4)
不使用变量而是将随机数放在列表中,然后使用sorted
或list.sort
对其进行排序:
>>> import random
使用list comprehension
创建包含随机项的列表:
>>> nums = [random.randrange(53)+1 for _ in xrange(5)]
>>> nums
[51, 49, 23, 27, 29]
sorted
:
>>> sorted(nums) #return a new sorted list, original list is not affected
[23, 27, 29, 49, 51]
list.sort
:
>>> nums.sort() #sort the list in-place
>>> nums
[23, 27, 29, 49, 51]
<强>代码:强>
pos = ['first', 'second', 'third', 'fourth', 'fifth']
for ball, ind in zip(sorted(nums), pos):
time.sleep(1)
print 'Your {} ball'.format(ind)
time.sleep(1)
print ball
答案 1 :(得分:0)
首先,您应该列出数字,而不是制作几个不同的变量。然后,您只需使用list.sort()
对数字进行排序。
numlist = []
for i in range(0, 5):
numlist.append(random.randrange(53)+1)
numlist.append(random.randrange(42)+1)
numlist.sort()