[Coursera][Rice] An introduction to interactive Programming in Python Week 2 3

一个十一基本上毁掉了很多事,总想着再说再说,懒的动懒的动,事情就这样一天天拖下去,日子就这么一天天过去,那么再读研的这三年又能有什么意义
Week 2 和 Week 3 全部错过了,严重拖延失败症

  • Week 2 - Event handlers, local variables, global variables, SimpleGui module, buttons, input fields
  • Week 3 - Canvas, event-driven drawing, string operations, drawing operations, timers

介绍了事件驱动编程,全局变量和局部变量,以及简单图形界面和交互

  • 运行窗体程序时,需导入 Special module for CodeSkulptor import SimpleGUI
  • 善用 CodeSkulptor 中的 Docs (需翻墙)
  • 注意在函数内使用全局变量需声明,否则会被当作局部变量出错。
  • and 操作符的优先级要大于or
  • 对于字符串,索引 -1 指向最后一个字符,以此类推。s[0:7] 从0 - 6 的字符不包括7。 word.str(35) 数字转化为字符串

Quiz 2a


Question 2

In CodeSkulptor, how many event handlers can be running at the same time?

1

Question 3

What are the three parts of a frame?

Canvas , Status Area, Control Area

Quiz 2b


Quzi 3a


Quiz 3b


Question8


  
  
# Mystery computation in Python
# Takes input n and computes output named result
import simplegui
# global state
result = 1
iteration = 0
max_iterations = 10
# helper functions
def init(start):
"""Initializes n."""
global n
n = start
print "Input is", n
# timer callback
def update():
global n
print n
if n == 1 or n == 0:
timer.stop()
elif n % 2 == 0:
n /= 2
else:
n = n * 3 + 1
# register event handlers
timer = simplegui.create_timer(1, update)
# start program
init(23)
timer.start()
Question 6

Write a CodeSkulptor program that experiments with the function time.time(). (Remember to import time.) Determine what date and time corresponds to time zero. Enter the year of that date as a four digit number.

1970


  
  
import time
print 2014 - time.time() / 60 /60 /24 /365

Mini-project # 2 - "Guess the number" game"

我的程序
没人检查好悲剧。

实现猜数字游戏。额外功能:两个 button 指定随机数字的范围分别是[1,100],[1,1000]。
实现限制猜数字的次数,范围内对2取对数。
麻烦的地方主要在于程序在控制台的显示。×&%& ^_^


  
  
# template for "Guess the number" mini-project
# input will come from buttons and an input field
# all output for the game will be printed in the console
import simplegui
import random
import math
# helper function to start and restart the game
def new_game():
# initialize global variables used in your code here
global secret_number
secret_number = random.randrange(0, 100)
print "New game. Range is from 0 to 100"
global count
count = int(math.ceil(math.log(100, 2)))
print "Number of remaining guesses is " + str(count)
print ""
# define event handlers for control panel
def range100():
# button that changes the range to [0,100) and starts a new game
global secret_number
secret_number = random.randrange(0, 100)
print "New game. Range is from 0 to 100"
global count
count = int(math.ceil(math.log(100, 2)))
print "Number of remaining guesses is " + str(count)
print ""
def range1000():
# button that changes the range to [0,1000) and starts a new game
global secret_number
secret_number = random.randrange(0, 1000)
print "New game. Range is from 0 to 1000"
global count
count = int(math.ceil(math.log(1000, 2)))
print "Number of remaining guesses is " + str(count)
print ""
def input_guess(guess):
# main game logic goes here
num = int(guess)
print "Guess was " + guess
global count
count = count - 1
print "Number of remaining guesses is " + str(count)
if num < secret_number:
print "Higher"
elif num > secret_number:
print "Lower"
elif num == secret_number:
print "Correct"
print ""
new_game()
print ""
if count <= 0:
print "You ran out of guesses. The number was " + str(secret_number)
print ""
new_game()
# create frame
frame = simplegui.create_frame("Guess the number", 300, 200)
# register event handlers for control elements and start frame
inp = frame.add_input('Input:', input_guess, 50)
button1 = frame.add_button('Reset 1-100', range100)
button2 = frame.add_button('Reset 1-1000', range1000)
frame.start()
# call new_game
new_game()
# always remember to check your completed program against the grading rubric

​Mini-Project 3

本项目主要是编写一个类似秒表的物体。并实现一个极其简单的小游戏,即在归零时stop成功即加一分。如此而已。

别人家的代码
我的代码


  
  
# template for "Stopwatch: The Game"
import simplegui
# define global variables
time = 0
x = 0
y = 0
# define helper function format that converts time
# in tenths of seconds into formatted string A:BC.D
def format(t):
t1 = t % 10
t2 = t / 10 % 60
if t2 < 10:
t2 = "0" + str(t2)
t3 = t / 10 / 60
if t3 == 10:
button_Reset()
return str(t3) + ":" + str(t2) + ":" + str(t1)
# define event handlers for buttons; "Start", "Stop", "Reset"
def button_Start():
timer.start()
def button_Stop():
global x, y
if bool(timer.is_running()):
y = y + 1
if time % 10 == 0:
x = x + 1
timer.stop()
def button_Reset():
timer.stop()
global time
time = 0
global x, y
x = 0
y = 0
# define event handler for timer with 0.1 sec interval
def timer_handler():
global time
time = time + 1
# define draw handler
def draw_handler(canvas):
canvas.draw_text(format(time), (120, 95), 20, "white")
canvas.draw_text(str(x) + "/" + str(y), (250,30), 20, "green")
# create frame
frame = simplegui.create_frame("Stopwatch: The Game",300,200)
# register event handlers
timer = simplegui.create_timer(100, timer_handler)
frame.set_draw_handler(draw_handler)
buttonStart = frame.add_button('Start', button_Start)
buttonStop = frame.add_button('Stop', button_Stop)
buttonReset = frame.add_button('Reset', button_Reset)
# start frame
frame.start()
# Please remember to review the grading rubric
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值