莱斯大学Python课程Mini-project # 2 - "Guess the number" game

要求:https://class.coursera.org/interactivepython-004/human_grading/view/courses/972072/assessments/29/submissions

我做的跟它的要求似乎有点不一样。中间遇到一个问题,费了好大力,发现是在文本框输入内容后没有按回车尴尬http://www.codeskulptor.org/

菜鸟一定要坚持下去!现在去吃晚饭

# interactive application to guess number
import random
import simplegui

# the globals
message = "now guess!"
high=100
low=0
target_number=random.randrange(low,high)
your_input=100

# compare the target number with your input
def compare_and_change():
   
   global message
    
   if target_number==your_input:
        message="correct,good job"
       
        
   elif target_number<your_input:
        guessnow()
        message="your number is " +str(your_input)+" too large"
        
        
   elif target_number>your_input:
        guessnow()
        message="your number is " +str(your_input)+" too small"
        
        
        
def guessnow():
    input_handler
    compare_and_change


#define draw handler
def draw(canvas):
    canvas.draw_text(message,[10,150],16,"White")


# define an input field handler
def input_handler(text):
    global your_input
    global message
    your_input=float(text)
    message=str(your_input)
    if your_input<low:
         message= "your num is out of range,try again"
         input_handler(text)  
    elif your_input>high:
         message= "your num is out of range,try again" 
 

    
#creat frame
frame=simplegui.create_frame("Guess",300,200)

#registor event handler
frame.set_draw_handler(draw)
frame.add_input("guess a number,then press enter",input_handler,180)
frame.add_button("compare",compare_and_change,100)  

#start
frame.start()



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值