大家求助一个python致命的猜数字程序的问题

大家求助一个python猜数字程序的问题
大家能不能帮忙看一下面这个问题,我想了一下要用到下面的要求不是很好写,请高手帮忙了。。。
for example, say the computer picks the number 1130 and the user chooses
1024. The computer will respond with 1B 1C. The 1 is in the correct position
and so is a bull (1B) and the 0 is a correct digit but in the wrong position
and so is a cow (1C). On the other hand, if the user chooses 1031 then
the computer will respond with 2B 2C as the first 1 and the 3 are in the
correct positions (2B) and the second 1 and the 0 are correct digits in the
wrong position (2C). When the user finally guesses the correct number the
computer will respond with 4B 0C which signifies that the number has been
guessed and the game has ended.

>>> bulls_and_cows()
Welcome to Bulls and Cows
How many digits? 4
Allow repeated digits (y or n)? n
Guess: 1123
Repeated digits
Guess: 112y
Must be digits
Guess: 123
Wrong number of digits
Guess: 12345
Wrong number of digits
Guess: 1234
3B 0C
Guess: 1235
2B 0C
Guess: 1264
3B 0C
Guess: 1274
4B 0C
Got it! Do you want another game with 4 digits (y or n) n
How many digits? 4
Allow repeated digits (y or n)? y
2
Guess: 1123
0B 1C
Guess: 2245
0B 2C
Guess: 6627
0B 2C
Guess: 8872
2B 2C
Guess: 7892
4B 0C
Got it! Do you want another game with 4 digits (y or n) y
Guess: 1123
0B 2C
Guess: 4451
0B 2C
Guess: 6715
0B 1C
Guess: 2345
0B 3C
Guess:
Do you give up (y or n)? y
The secret number is 3274
Do you want another game with 4 digits (y or n) n
How many digits? 0
>>>
1  Tasks
For each function that you write you need to provide a suitable comment
giving a description, the type and any preconditions. You should use the
triple-quote commenting style.
3

1.2 Write the code
Finally, write your solution to the assignment making sure you have included
suitable comments.
As you can see from the example above the main entry point is the function
bulls_and_cows. When called, this function provides the required interface.
To support this function you need to write the following four functions.
• make_secret(numdigits, repeat) that takes the number of digits for
the secret number and whether or not repeated digits are allowed and
returns the list of digit characters as the secret. A list is used rather
than a number for two reasons. First, it’s difficult to use a number when
we want leading zeros and second, it’s easier to compare the secret as
a list of digit characters with the users guess. This function should use
the random number module to generate the secret numbers.
Here are some examples (remember that this is generating random
numbers).
>>> make_secret(4, True)
[’1’, ’7’, ’8’, ’3’]
>>> make_secret(4, True)
[’1’, ’9’, ’2’, ’2’]
>>> make_secret(4, False)
[’3’, ’7’, ’0’, ’5’]
>>> make_secret(4, False)
[’2’, ’7’, ’1’, ’9’]
>>>
• score(secret, guess) that takes two lists of the same length and
returns, as a pair, the number of bulls and cows.
4
Here are some examples.
>>> score([’2’, ’7’, ’1’, ’9’], [’2’, ’1’, ’3’, ’5’])
(1, 1)
>>> score([’1’, ’9’, ’2’, ’2’], [’1’, ’3’, ’2’, ’3’])
(2, 0)
>>> score([’1’, ’9’, ’2’, ’2’], [’2’, ’2’, ’2’, ’3’])
(1, 2)
>>>
• check_guess(numdigits, repeat, guess) that takes the number of
digits of the secret, whether repeats are allowed and the user guess as a
list of digit characters and returns True if the guess is a valid guess or
False if the guess is invalid - i.e. it has the wrong number of digits, it
contains non-digit characters or (if repeats are not allowed) it contains
repeated digit characters. As a side effect it prints an error message of
the appropriate kind if an error is detected.
Here are some examples.
>>> check_guess(4, True, [’1’,’1’,’1’,’1’])
True
>>> check_guess(4, True, [’1’,’1’,’1’,’x’])
Must be digits
False
>>> check_guess(4, False, [’1’,’1’,’1’,’1’])
Repeated digits
False
>>> check_guess(4, False, [’1’,’2’,’3’])
Wrong number of digits
False
>>> check_guess(4, False, [’1’,’2’,’3’, ’4’])
True
>>>
• make_guess(numdigits, repeats, secret) that takes the number
of digits, whether repeats are allowed and the secret as a list of digit
characters. It is responsible for getting a guess from the user, checking
5
if the guess is empty and if not calculating and displaying the number
of bulls and cows and determining if the game is over. The function
should return one of three characters: ’c’ if the current game should
continue, ’y’ if the current game should end (either because the user
has guessed the secret or has given up) but the user wants another
game of the current style and ’n’ if the current game should end but
the user does not want another game of the current style.
Here are some examples.
>>> make_guess(4, False, [’1’, ’2’, ’3’, ’4’])
Guess: 2134
2B 2C
’c’
>>> make_guess(4, False, [’1’, ’2’, ’3’, ’4’])
Guess:
Do you give up (y or n)? n
’c’
>>> make_guess(4, False, [’1’, ’2’, ’3’, ’4’])
Guess:
Do you give up (y or n)? y
The secret number is 1234
Do you want another game with 4 digits (y or n) n
’n’
>>> make_guess(4, False, [’1’, ’2’, ’3’, ’4’])
Guess: 1234
4B 0C
Got it! Do you want another game with 4 digits (y or n) y
’y’
>>>
Note that the ’c’, ’n’ and ’y’ above are the displayed returns of the
function while the other parts of the display are due to the interaction
with the user.
6
You will find that the four functions above can be used either directly or
indirectly in the definition of the top-level function bulls_and_cows.
我也来说两句 查看全部回复

最新回复

  • defg934 (2008-8-23 13:56:38)

    新人报道,前辈多多关照!!!支持!!呵呵

















    上海壹琦生产经营涂层测厚仪兆欧表温湿度计激光测距仪等各种仪器仪表
  • dzmms36 (2008-8-24 22:40:15)

  • cdef362 (2008-8-26 00:10:07)

    博客版面可以自主设计,也是我喜欢的原因。下边我介绍一点点编辑博客的经验给大家分享。

    在自己博客中添加友情链接的 步骤:

    1.用自己帐号和密码登录到博客后,点击博客首页 左上角的 绿色 向下的箭头 ------粉装博客-----设置首页内容

    2.添加自定义模块,主题名为友情连接,添加后会显示在 博客主页里

    3.点击 自助友情链接 模块右上角的编辑,把以下代码复制进去就好了。
  • dzmms36 (2008-8-26 00:56:37)

    不管菲尔普斯和他的美国游泳队友们是否喜欢比萨和意大利面,他们都可以在未来整整一年中尽情享受这种西方人喜爱的美食,而且不用考虑任何费用问题。

      四川新闻网记者8月19日获悉,早在菲尔普斯赢得六金时 比萨加盟,美国必胜客总裁就曾向这位百年奥运史上最伟大的运动员作出保证,只要他能如愿夺取八枚奥运金牌,必胜客就将向整个美国游泳队免费提供一年的比萨和意大利面。

      8月14日是菲尔普斯西餐加盟在北京奥运会上夺取第六枚金牌的纪念日,必胜客总裁这天在给菲尔普斯的信中写到,“尊敬的菲尔普斯先生,祝贺您为您的祖国连续赢得多枚金牌!您的比萨加盟坚强毅力和奥运精神为美国赢得了荣耀,鼓舞了世人。”

      信中说,“我们最近刚得知您经常食用比萨和意大利面为比赛补充能量。以提供金牌标准的比萨和意大利面为己任的必胜客,愿
    比萨加盟意在您继续努力追求更好成绩的同时为您提供一些鼓励。如果您能在今年获得八枚金牌而打破马克施皮茨的纪录,我们将为您和您所在的美国游泳队免费提供一整年的比google优化
    萨和意大利面。现在我们衷心祝愿您好运!无论结果如何,您都是我们心目中的冠军。此外,如果您想念家乡的口味了,我们希望您知道在北京有五十三家必胜客餐厅等候您的光临。”

      “现在是到兑现承诺的时候了”,必胜客方面说。
  • defg934 (2008-8-27 10:23:29)

    新人报道,前辈多多关照!!!支持!!呵呵
















    玩具加盟,玩具店加盟
  • defg934 (2008-8-29 20:09:26)

    不错!支持!!呵呵














    强弓男性治疗仪是根据美国数字阴茎增大 器和日本男性封闭循环式保育器技术,经国家食品药品监督管理局批准生产的国家准字号医疗器械。强弓男性治疗仪通过数控真空牵引,高频远红外电磁,脉冲水流按摩等功能,具有延长男性生殖器悬韧带,增加海绵体血容量。适用范围:阳痿、早泄阴茎延长 、中老年阴茎萎缩、慢性前列腺炎。
  • dzmms36 (2008-9-01 23:14:13)

    公司拥有先进的真空熔化、真空铸造、气体保护焊及热处理设备与工艺,并配有完善的检测系统,长期从事该领域的人员拥有多铸造铜冷却壁
    自耗炉坩埚
    大转炉氧枪喷头
    电机端环
    电机导条项专利及技术诀窍,长年来,万隆公司始终与各大科研院校进行技术交流与合作,产品一直处于同行业领先地位。
  • cdef362 (2008-9-02 00:39:29)

    有很多新站特别喜欢交换链接的,尤其是和pr高的站点友情链接,疯狂的缠着我,要我带带他们的站点,这样pr成了建立友情链的入场券。记得以前我也疯狂的寻求友情链接,后来不知为何好多人主动加我,要求互换,导致现在大量的链接没有友情,仅仅是面子链接。是的,仅仅因为我在的这家站点的pr高了。

    在建立友情连接的时候,很多人都会把PR和站的质量结合起来,其实PR只能作为网站质量的一个参数,这个参数是可以通过各种途径获取的,不是真正意义上的搜索引擎给予的评价。

    可是如果站点的PR=0或者比较低,很多人都会不理睬你,任凭你怎么说,对pr高的站点来说,都不会和你自助友情链接,因为他们认为你会拖累他们。假如你的PR比较高,甚至达到PR5,6,7,那时候,很多站点都会跟在你的后面,以一种羡慕的眼光看着你,期许着你能带带他的小站。
  • hijk053 (2008-9-02 19:52:25)

    支持!!呵呵  















    湖北led---武汉鸿泰达电子有限责任公司是专业致力于LED电子显示屏行业,集研制、开发、软件支持、施工安装、营销和售后服务于一体的高新技术企业。
  • hijk053 (2008-9-04 11:48:46)

    新人报道,前辈多多关照!!!支持!!呵呵















    苏州易维试验设备有限公司是一家集科研、设计及制造各类模拟环境试验恒温恒湿试验箱高低温试验箱盐雾试验箱的专业性企业。
  • zhengshicai125 (2008-9-04 12:26:16)

    让世界都拥有一个家
    代孕是指在需求女方完全丧失生育能力的前提下,将其卵子(或代孕志愿方卵子)与丈夫的 精子结合成受精卵, 在代孕志愿方子宫完成整个孕育过程并顺利生产的爱心互助自愿行为。代孕分为体外受 精(试管婴儿)和人工授精俩种方式。没有任何身体接触,不触犯任何法律。
    代孕充分保障了不育女性和健康丈夫的生育人权。血脉的延续和完整家庭的观念是全世界人 类的共 识。爱心代孕完全发扬了人类崇高的人性互助关怀的人道主义精神!是无可非议的爱心道德行为!随 着中国文明进程的加快, 弱势群体理当得到公权机关的正确关注和帮助,爱心代孕行为必将逐步得到 中国全社会的完全、共同认可!
    代孕的意义:它间接杜绝了部分贩卖、偷盗婴儿的罪恶行为;它用最有效的爱心互助方式 帮助了 中国不育家庭和经济较困难家庭;它杜绝了国内相当多的医院对不育患者的故意误诊、加重病情、 榨 取钱财的罪恶行为;它解决了收养制度上的昂贵开支、烦琐程序、漫长等待、血缘遗憾、质量缺陷;它保证了中华 优秀血脉的延续和比例!(需求者大多为高学历、高素质、高地位。)等等更多.
    代孕指代他人怀孕及生育,是指女性接受他人委托,用人工辅助生育方式为他人生育子女的行为。代为他人生育的女性通常称为代理孕母(也被称为代孕妈妈, 代孕母亲,代母),委托他人生育子女的人被称为委托方(也称为委托人或委托父母)。

    代孕不涉及性关系,因而它是属于人工生殖技术的一种。代孕一般分为四种:一为精子、卵子均来自于夫妻双方,借用代理孕母的子宫;二是精子来自丈夫,卵子由 代孕者提供,经体外授精(试管婴儿)后,由 代孕者怀孕生育;三是精子来自需求方,卵子由代理孕母提供,经人工授精后,由 代孕者怀孕生育;三为卵子由妻子提供,经异质人工授精后通过胚胎移植由 代孕者生育。
    == 代孕的方式==
    1、人工受精 代孕
    人工受精方式 代孕是指通过医学辅助手段将需求方的精子送入 代孕志愿者的体内,在体内进行受精,并完成怀孕全过程,这种方式的孩子是和 代孕者有遗传关系的。
    2、试管婴儿 代孕
    试管婴儿 代孕就是使用需求方的卵子和精子,做成试管胚胎,植入代孕志愿者的体内完成怀孕的全过程.

    代孕一般分为四种:一为精子、卵子均来自于夫妻双方,借用志愿者的子宫;二是精子来自丈夫,卵子由 代孕者提供,经体外授精(试管婴儿)后。由 代孕者怀孕生育;三是精子来自需求方,卵子由志愿者提供,经人工授精后。由代孕者怀孕生育;四为卵子由妻子提供,经异质人工授精后通过胚胎移植由 代孕者生育 。
  • dzmms36 (2008-9-13 12:11:31)

    Marketers也许需要做对他们的搜索引擎优化(
    seo )竞选的变动如果谷歌的建议工具证明成功在它的主页,根据E咨询学校的格雷姆Charlton。

    The专家说工具,提供用户查寻的建议名单,当他们键入他们自己的询问入Google.com查寻箱子,也许带领企业校正他们怎么接近seo服务

    This在谷歌建议倾向于提供与几个词的询问在他们, Charlton先生解释的事实可能包括合并主题词的大数入竞选
    seo咨询 大写。

    It可能也意味依靠的结尾以一些网站交通的被拼错的方式,他注意了,谷歌建议自动地改正拼错。

    Mr在长尾巴主题词的涵义也接触北京google优化 的Charlton,陈述: “谷歌提供根据词组大众化的查寻建议搜寻为,那么更加阴暗,长尾巴期限是很少可能出现您开始键入”。
    当收集信息为校对的google优化 建议的目的时, Google最近修正工具进一步保护用户数据。
  • dzmms36 (2008-9-14 15:20:39)

    他统计地有更好的季节- “我在2004年在六竞选中获胜,记得?” 显露的-,但是Earnhardt这个星期他相信他丢失了冠军那年,因为他未准备着赢取一。 他在晒干的决赛点身分,追逐的第一该年完成了第五,并且可以急促背诵他犯在舒展下采取他在标题角逐外面错误的名单。

    He为Dale Earnhardt Inc.那时仍然驾驶了,并且在这个季节之前,是他的前个合法的机会获得标题。 他在2005年想念追逐,在20洗车机
    06年是一个非因素第五和再丢失它去年在他驾驶为他的已故的父亲的种族队的最后的季节。

    Now他是以强大Hendrick Motorsports, Earnhardt有从压力和戏曲的设备、支持和解放他备鞍了与在DEI。

    Everything为Earnhardt自动洗车机
    是最后相等的,亲自和专业地,并且它显示。

    "我认为他今年更大量有信心, “斯图尔特说两次联赛冠军托尼・。 “他总是确信轨道,但是我认为您看见它在驾驶在轨道的他的他有我不认为我们看了过去几年的更新的信心”。

    Kevin Harvick,有接收跟随他的2001烟台搬厂
    年死亡的晚Earnhardt的乘驾不值得羡慕的任务,也注意了在小辈上的一个变化他视同对切口领带与DEI和混乱的关系他有与继母的这个季节,特里萨。

    "他比什么他曾经是, “Harvick似乎更大量轻松说。 “更大量大概有与家业的重音并且必须回答到他的爸爸的名字,并且我可以种烟台设备搬运
    类与那关连。 进入他在他自己的事业更多控制的情况,并且它比情况一定感到舒适和有意义他以前是”。

    The压力仍然依然存在,虽然烟台起重安装
    ,以后的正方形在他从要求七时间冠军儿子加标题或二到他的名字的一个狂热风扇基地。
  • efgh451 (2008-10-04 11:18:50)

    勇敢与持枪劫匪搏斗 巴西华侨拼死保住赈灾捐款





















    专业生产混合机,混合器,行星动力混合机,均质机,反应器,乳化机,螺条式混合机,锥形混合机,卧式混合机,立式混合机,行星动力混合机无重力混合机 无重力混合机 无重力混合机,管道乳化机,连续混合机等混合设备
  • efgh451 (2008-10-22 15:32:55)

    新手报道请大家多多包含!!呵呵   希望;论坛越办越好!!!!!





















    nail equipment nail products nail product nail uv lamp nail uv lamp