自学python 3.3.0,课后练习遇到难题,请各位帮忙.

软件和网站开发以及相关技术探讨
回复
drongh
帖子: 1038
注册时间: 2007-01-10 9:32

自学python 3.3.0,课后练习遇到难题,请各位帮忙.

#1

帖子 drongh » 2013-01-29 11:26

Chapter 1 The way of the program
Chapter 2 Variables, expressions, and statements
Chapter 3 Hello, little turtles!
Chapter 4 Functions
Chapter 5 Conditionals
Chapter 6 Fruitful functions
Chapter 7 Iteration
Chapter 8 Strings
Chapter 9 Tuples
Chapter 10 Event handling

上面是我已经学过的内容.下面是题目.请高手帮忙.

书名是:How to Think Like a Computer Scientist: Learning with Python 3.
题目是第十章最后一题.
附件
练习题.PNG
drongh
帖子: 1038
注册时间: 2007-01-10 9:32

Re: 自学python 3.3.0,课后练习遇到难题,请各位帮忙.

#2

帖子 drongh » 2013-01-30 8:09

自己顶自己罗.
头像
cuihao
帖子: 4793
注册时间: 2008-07-24 11:33
来自: 郑州
联系:

Re: 自学python 3.3.0,课后练习遇到难题,请各位帮忙.

#3

帖子 cuihao » 2013-01-30 8:42

这题和python有啥关系?自己去看看网球规则,画个图嘛,没有编程要求……
求人不如求它仨: 天蓝的Wiki 屎黄的Wiki 绿
Site: CUIHAO.TK    Twitter: @cuihaoleo
Machine: Athlon64 X2 5200+ / 2x2GB DDR2-800 / GeForce GTS 450
AD: ~まだ見ぬ誰かの笑顔のために~
头像
cuihao
帖子: 4793
注册时间: 2008-07-24 11:33
来自: 郑州
联系:

Re: 自学python 3.3.0,课后练习遇到难题,请各位帮忙.

#4

帖子 cuihao » 2013-01-30 8:44

……难道是用程序绘制?
求人不如求它仨: 天蓝的Wiki 屎黄的Wiki 绿
Site: CUIHAO.TK    Twitter: @cuihaoleo
Machine: Athlon64 X2 5200+ / 2x2GB DDR2-800 / GeForce GTS 450
AD: ~まだ見ぬ誰かの笑顔のために~
drongh
帖子: 1038
注册时间: 2007-01-10 9:32

Re: 自学python 3.3.0,课后练习遇到难题,请各位帮忙.

#5

帖子 drongh » 2013-01-30 10:15

这是个编程题,当然要用python编写,列出所有的状态和条件.
头像
cuihao
帖子: 4793
注册时间: 2008-07-24 11:33
来自: 郑州
联系:

Re: 自学python 3.3.0,课后练习遇到难题,请各位帮忙.

#6

帖子 cuihao » 2013-01-30 10:20

难点在哪里?
如果只是列举状态,递归搜索不就可以了?
还是不会规则?那我也无能为力,o(╯□╰)o
求人不如求它仨: 天蓝的Wiki 屎黄的Wiki 绿
Site: CUIHAO.TK    Twitter: @cuihaoleo
Machine: Athlon64 X2 5200+ / 2x2GB DDR2-800 / GeForce GTS 450
AD: ~まだ見ぬ誰かの笑顔のために~
drongh
帖子: 1038
注册时间: 2007-01-10 9:32

Re: 自学python 3.3.0,课后练习遇到难题,请各位帮忙.

#7

帖子 drongh » 2013-01-30 10:35

代码: 全选

import turtle           # Tess becomes a traffic light.

turtle.setup(400,500)
wn = turtle.Screen()
wn.title("Tess becomes a traffic light!")
wn.bgcolor("lightgreen")
tess = turtle.Turtle()


def draw_housing():
    """ Draw a nice housing to hold the traffic lights """
    tess.pensize(3)
    tess.color("black", "darkgrey")
    tess.begin_fill()
    tess.forward(80)
    tess.left(90)
    tess.forward(200)
    tess.circle(40, 180)
    tess.forward(200)
    tess.left(90)
    tess.end_fill()


draw_housing()

tess.penup()
# Position tess onto the place where the green light should be
tess.forward(40)
tess.left(90)
tess.forward(50)
# Turn tess into a big green circle
tess.shape("circle")
tess.shapesize(3)
tess.fillcolor("green")

# A traffic light is a kind of state machine with three states,
# Green, Orange, Red.  We number these states  0, 1, 2
# When the machine changes state, we change tess' position and
# her fillcolor.

# This variable holds the current state of the machine
state_num = 0


def advance_state_machine():
    global state_num
    if state_num == 0:       # Transition from state 0 to state 1
        tess.forward(70)
        tess.fillcolor("orange")
        state_num = 1
    elif state_num == 1:     # Transition from state 1 to state 2
        tess.forward(70)
        tess.fillcolor("red")
        state_num = 2
    else:                    # Transition from state 2 to state 0
        tess.back(140)
        tess.fillcolor("green")
        state_num = 0

# Bind the event handler to the space key.
wn.onkey(advance_state_machine, "space")

wn.listen()                      # Listen for events
wn.mainloop()
上面的代码上书本上的例题,我想也差不多.

楼上的,能否就前面3个状态给出一点代码,我是新手.
头像
cuihao
帖子: 4793
注册时间: 2008-07-24 11:33
来自: 郑州
联系:

Re: 自学python 3.3.0,课后练习遇到难题,请各位帮忙.

#8

帖子 cuihao » 2013-01-30 10:53

看了看这书……怎么说呢,不是一般的编程语言入门书。
我不会他这个绘图模块,所以写不出那样的程序。
求人不如求它仨: 天蓝的Wiki 屎黄的Wiki 绿
Site: CUIHAO.TK    Twitter: @cuihaoleo
Machine: Athlon64 X2 5200+ / 2x2GB DDR2-800 / GeForce GTS 450
AD: ~まだ見ぬ誰かの笑顔のために~
回复