TA的每日心情 | 擦汗 3 天前 |
---|
签到天数: 527 天 连续签到: 4 天 [LV.9]测试副司令
|
1测试积点
有两段python代码,运行应该说会一致的,但是没有达到一致的效果,不知道问题在哪,其中car1.py输出是对的.
- <p>car1.py:</p><p>from random import random</p><p>time = 5
- car_positions = [1,1,1]</p><p>while time:
- # decrease time
- time -= 1</p><p> print ('')
- for i in range(len(car_positions)):
- # move car
- if random() > 0.3:
- car_positions[i] += 1</p><p>
- # draw car
- print ('-' * car_positions[i])</p>
复制代码
用函数式的写法:
- <p>car_function.py: (执行的输出不太对)
- from random import random</p><p>def race(state):
- draw(state)
- if state['time']:
- race(run_step_of_race(state))</p><p>def draw(state):
- print ('')
- print ('\n'.join(map(output_car,state['car_positions'])))</p><p>def output_car(car_positions):
- return '-' * car_positions</p><p>def run_step_of_race(state):
- return {'time':state['time']-1,'car_positions':move_cars(state['car_positions'])}</p><p>def move_cars(car_positions):
- return map(lambda x : x+1 if random() > 0.3 else x,car_positions)</p><p> </p><p>race({'time':5,'car_positions':[1,1,1]})</p>
复制代码
|
|