#!/usr/bin/env python # File: numgame.py # Author: gosman # Date: 2007-7-15 import random step = 0 numarray = range(1,10) list = range(1,10) random.shuffle(list) while not(list == numarray): for i in range(9): print list[i], step = step + 1 index = raw_input('\nReverse how many? ') index = int(index) tmp = list[0:index] tmp.reverse() list = tmp + list[index:] print 'Done ! That took you %d steps.' % step |
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 |
numbers[:index] = numbers[index-1::-1] |
欢迎光临 Chinaunix (http://bbs.chinaunix.net/) | Powered by Discuz! X3.2 |