【发布时间】:2013-02-16 22:37:28
【问题描述】:
我是 Python 的新手,没有编程经验。我有这个(我不确定它是列表还是数组):
from random import choice
while True:
s=['The smell of flowers',
'I remember our first house',
'Will you ever forgive me?',
'I\'ve done things I\'m not proud of',
'I turn my head towards the clouds',
'This is the end',
'The sensation of falling',
'Old friends that have said good bye',
'I\'m alone',
'Dreams unrealized',
'We used to be happy',
'Nothing is the same',
'I find someone new',
'I\'m happy',
'I lie',
]
l=choice(range(5,10))
while len(s)>l:
s.remove(choice(s))
print "\nFalling:\n"+'.\n'.join(s)+'.'
raw_input('')
其中随机选择5-10行打印,但打印顺序相同;即“我说谎”如果被选中将始终位于底部。我想知道如何打乱选定的行,使它们以更随机的顺序出现?
编辑: 所以当我尝试运行这个时:
import random
s=['The smell of flowers',
'I remember our first house',
'Will you ever forgive me?',
'I\'ve done things I\'m not proud of',
'I turn my head towards the clouds',
'This is the end',
'The sensation of falling',
'Old friends that have said good bye',
'I\'m alone',
'Dreams unrealized',
'We used to be happy',
'Nothing is the same',
'I find someone new',
'I\'m happy',
'I lie',
]
picked=random.sample(s,random.randint(5,10))
print "\nFalling:\n"+'.\n'.join(picked)+'.'
它似乎在运行,但没有打印任何东西。我从 Amber 的回答中正确输入了吗?我真的不知道我在做什么。
【问题讨论】:
-
您的代码随机选择行并删除它们。