我有一个查询只返回最后的结果,所以这是我的代码:
import random
def Step_1():
Start_Game = raw_input("Enter \'Start' to continue \n")
if Start_Game == 'start':
print "Let the Lottery begin"
else:
return Step_1()
#-------------------------------------------------------------------
def Step_2():
random_list = []
for i in range(10):
while len(random_list) < 6:
random_number = random.randint(1,59)
while random_number not in random_list:
random_list.append(random_number)
print random_list
Step_1()
Step_2()
当我运行它时,它给我以下结果,
Enter 'Start' to continue
start
Let the Lottery begin
[56]
[56, 20]
[56, 20, 32]
[56, 20, 32, 2]
[56, 20, 32, 2, 23]
[56, 20, 32, 2, 23, 30]
但是,我怎样才能显示最后生成的结果并丢弃之前的5.我知道我需要在Step_2函数中更改最后一位“print”,但我需要添加什么?
谢谢。
答案 0 :(得分:0)
要使Step_2
函数仅打印列表的最终版本,您只需将print
语句移到循环之外(取消它)。但是你有两个以上的循环,所以你可以简化一些事情:
def Step_2():
random_list = []
while len(random_list) < 6: # the for loop didn't do anything useful, delete it
random_number = random.randint(1,59)
if random_number not in random_list: # use an if instead of another while loop
random_list.append(random_number)
print random_list # unindent this line so it only prints when the loop is done