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

第三周的作业,大致就是两个给定的上下界,在其中随机一个数,让用的人输入猜的数字,告知他实际的数字比猜的大还是小,一共有[log(upper) / log(2)]次机会,策略是二分。

这次是带界面的,库是老师写的simplegui,在下面的链接里(科学上网)

现在完成了第一部分。输出还是在控制台而不是在画布。。

http://www.codeskulptor.org

思路大致是这样:

  用全局变量存上下界,通过按钮的动作给出。每次一开始调用newgame做各种预处理,随机出给定的数,计算剩余剩余猜的次数等等。每次按按钮也要记得调用newgame

  打分的时候发现有人一开始没调用。。貌似是不行的吧?

  接下来输入的时候获取输入值判断什么的。。次数没了要重新开始。

  用了try防止不合法输入。。自己玩起来还没有什么问题。除了不太方便。。。。。= =

体会是:

  python的全局变量。。。是要在函数里声明的。。

  开发貌似很练码力。。。。。。。。。。。。。。。。。。。。。。。。。。。。。

 1 # template for "Guess the number" mini-project
 2 # input will come from buttons and an input field
 3 # all output for the game will be printed in the console
 4
 5 import simplegui
 6 import random
 7 import math
 8 guess_num = 0
 9 secret_number = 0
10
11 upper = 100
12 remain_chance = 7
13
14 # helper function to start and restart the game
15 def new_game():
16     # initialize global variables used in your code here
17     global secret_number, remain_chance
18     # remove this when you add your code
19     secret_number = random.randrange(0, upper)
20     remain_chance = int(math.ceil(math.log(upper)/math.log(2)))
21     print "New game start:","Range is [0,", upper,")"
22     print "Number of remaining guesses is", remain_chance
23
24 # define event handlers for control panel
25 def range100():
26     # button that changes the range to [0,100) and starts a new game
27     global upper, remain_chance
28     # remove this when you add your code
29     upper = 100
30     remain_chance = 7
31     new_game()
32
33 def range1000():
34     # button that changes the range to [0,1000) and starts a new game
35     global upper
36     remain_chance = 10
37     upper = 1000
38     new_game()
39
40
41 def input_guess(guess):
42     # main game logic goes here
43     global guess_num, remain_chance
44     # remove this when you add your code
45     try:
46         guess_num = int(guess)
47     except:
48         print "an integer please"
49         return
50     print "Guess was", guess
51     remain_chance -= 1
52     print "Number of remaining guesses is", remain_chance
53     #judge
54     if guess_num == secret_number:
55         print "Correct"
56         new_game()
57         return
58     elif guess_num > secret_number: print "Lower"
59     elif guess_num < secret_number: print "Higher"
60
61     #if run out of chance
62
63     if remain_chance == 0:
64         print "You ran out of chances"
65         new_game()
66
67
68 # call new_game
69 new_game()
70
71
72 # create frame
73 f = simplegui.create_frame("Guess", 200, 200)
74
75
76 # register event handlers for control elements and start frame
77 input_guess = f.add_input("enter what you guess", input_guess, 100)
78 reset = f.add_button("reset", new_game)
79 Range_100 = f.add_button("Range is [0,100)", range100)
80 Range_1000 = f.add_button("Range is [0,1000)", range1000)
81 f.start()
82
83 # always remember to check your completed program against the grading rubric

时间: 2024-10-05 16:50:01

Mini-project # 2 - "Guess the number" game"的相关文章

Project Euler: Problem 17 Number letter counts

If the numbers 1 to 5 are written out in words: one, two, three, four, five, then there are 3 + 3 + 5 + 4 + 4 = 19 letters used in total. If all the numbers from 1 to 1000 (one thousand) inclusive were written out in words, how many letters would be

纸牌project

用range[0,8)的列表表示牌,这些数字要出现两次.我们建议你通过连接两个range[0,8)的列表来创建这个list.利用Docs来安排列表串联操作 写一个draw handler啥样的draw handler呢?用for循环来迭代Memory deck,并用draw_text在画布上画出表示每个卡片的数字.结果应该是一个水平均等间隔的数字序列画在画布上 用random.shuffle()来洗牌.在洗牌之前记得消灭你canvas drawing代码的bug,这样的话消灭bug会比较容易一点

Crystal Clear Applied: The Seven Properties of Running an Agile Project (转载)

作者Alistair Cockburn, Crystal Clear的7个成功要素,写得挺好. 敏捷方法的关注点,大家可以参考,太激动所以转载了. 原文:http://www.informit.com/articles/article.aspx?p=345009 Property 1. Frequent Delivery The single most important property of any project, large or small, agile or not, is that

Mini projects ---- Stopwatch: The Game

课程全名:An Introduction to Interactive Programming in Python,来自 Rice University 授课教授:Joe Warren, Scott Rixner, John Greiner, Stephen Wong 工具:http://www.codeskulptor.org/, simplegui 模块 这是第三次作业,前面两次主要是熟悉Python动手做起来都很简单,就不记录了. 作业需要完成一个关于手表的游戏,估计很多人也都玩过.初中和

1398444 - Buffering the document number assignment for RF_BELEG

Version 7   Type SAP Note Release Status Released for Customer   Language English Responsible Volker Kleinhenz ( D024958 )   Masterlanguage German Processor Volker Kleinhenz ( D024958 )   Last Changed On 13.02.2017 12:37:36 Component FI-GL-GL-A ( Pos

Principle of Computing (Python)学习笔记(7) DFS Search + Tic Tac Toe use MiniMax Stratedy

1. Trees Tree is a recursive structure. 1.1 math nodes https://class.coursera.org/principlescomputing-001/wiki/view? page=trees 1.2 CODE无parent域的树 http://www.codeskulptor.org/#poc_tree.py class Tree: """ Recursive definition for trees plus

自动部署工具ant

学习ant所做的笔记,根据官方网站所举事例 <?xml version="1.0" ?> <project name="ant-project" default="print-dir"> <property name="name" value="jojo" /> <target name="print-dir"> <echo mess

8.Hibernate的多对多关联映射

1.创建如下数据库脚本 1 --2.项目表 2 -- Create table 3 create table PROJECT 4 ( 5 PROID NUMBER(6) not null, 6 PRONAME VARCHAR2(50) 7 ) 8 tablespace USERS 9 pctfree 10 10 initrans 1 11 maxtrans 255 12 storage 13 ( 14 initial 64 15 minextents 1 16 maxextents unlimi

[转载] CMake Official Tutorial——教程还是官方的好

CMake官方教程传送门:https://cmake.org/cmake-tutorial/ 以下的内容跟官方教程基本一致,少数地方根据自己的测试有所改动: A Basic Starting Point (Step1) The most basic project is an executable built from source code files. For simple projects a two line CMakeLists.txt file is all that is requ

【流媒体开发】VLC Media Player - Android 平台源码编译 与 二次开发详解 (提供详细800M下载好的编译源码及eclipse可调试播放器源码下载)

作者 : 韩曙亮  博客地址 : http://blog.csdn.net/shulianghan/article/details/42707293 转载请注明出处 : http://blog.csdn.net/shulianghan VLC 二次开发 视频教程 : http://edu.csdn.net/course/detail/355 博客总结 : -- 本博客目的 : 让 Android 开发者通过看本博客能够掌握独立移植 VLC Media Player 核心框架到自己的 app 中,