- 论坛徽章:
- 0
|
以下是我的 code , 多多指教, 不知道可以不可以再精簡一點!!
import random
# generate a list of numbers 1-9 in random order
# for instance: [3,5,7,1,2,9,6,8,4]
numbers = random.sample(range(1,10), 9)
steps = 0
# numerbs != [1,2,3,4,5,6,7,8,9]
while numbers != range(1,10):
# join the all 9 numbers together with a space
# for instance: 2 9 3 6 1 8 7 5 4
# or use ' '.join( str(n) for n in numbers )
print ' '.join(map(str,numbers))
index = int(raw_input('Reverse how many? '))
# here is the main part of the game.
# index-1 is the range of the numbers you select,
# and -1 is to reverse the selected numbers.
numbers[:index] = numbers[index-1::-1]
steps += 1
print 'Done! That took you %d steps.' % steps |
[ 本帖最后由 eookoo 于 2007-7-17 00:20 编辑 ] |
|