|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
原题:
文曲星上的小游戏:猜一个四位数,没有相同数字.用户每猜一个数字,显示出“完全猜中的数字个数”和“猜中数字但位置错误的数字个数”,比如nAmB,数字n表示猜中的位置正确的数字个数,数字m表示数字正确而位置不对的数字个数。例如,正确答案为3864,如果用户猜8234,则显示:1A2B,数字1表示数字4及其位置猜对了,数字3和8这两个数字猜对了,但是位置没对,记为2B。然后,用户根据游戏提示的信息继续猜,直到猜中为止。设计思路产生随机数0-9,用列表存储以字符串类型的数字,不涉及计算 字符串 str,;输入数字,用while循环判断输入的数字是否符合要求,4个不同的数.列表转化集合用于删除列表中相同的值(即重复的数字).每猜一次计次数,可以设计成绩,越少用猜对,成绩越高.
答案:
- import random as r
- def creatNums(): # 定义一个函数,用于随机取数
- n=4 # 一共需要取4次
- numbers = [] # 设置一个空列表
- while n: # 当 n 大于0时
- number = str(r.randint(0,9)) # 随机取一个数
- while number in numbers: # 当number出现时,重复了列表numbers中的数字时
- number = str(r.randint(0,9)) # 二次,随机取一个数,循环至数字不重复
- numbers.append(number) # 把 number 放入列表
- n -= 1 # 放一个数,减少一次
- return (numbers) # 返回 numbers
- def numjud(numbers, gnums): # 定义一个函数,用于评判(答案数字,猜测的数字)
- i = npc = nc = 0 # 这三个值,起始值为零
- for n in gnums: # 猜测中的每一个数字
- if n in numbers: # 如果列表中,本数字存在
- nc += 1 # 数字猜对的值加一
- if numbers[i] == gnums[i]: # 如果列表中第0个数字值与位置与猜测值相同
- npc += 1 # 数字和位置均猜对,值加一
- nc -= 1 # 数字猜对的值,减回来一个
- i += 1 # 向后移动一位,进行下一个判断
- print('{}A{}B'.format(npc, nc))
- return npc # 本句的必要性是什么?为何要单独返回 npc的值?而不需要返回 nc 的值?
- print('游戏开始,请你猜四个数字(0-9):')
- numbers = creatNums()
- npc = 0
- guesstimes = 0
- while npc<4:
- while True: # 本句code,必要吗?不写,不能运行?
- inputnums = input('请输入四个不同的数字:')
- if len(inputnums) == 4 and inputnums.isdigit():
- gnums = list(inputnums)
- if len(set(gnums)) == 4:
- # set() 函数,可创建无序不重复元素集
- break
- else:
- print('数字不能重复')
- else:
- print('输入有误!')
- gnums = list(inputnums)
- npc = numjud(numbers, gnums)
- guesstimes += 1
- if guesstimes < 8:
- print('你真棒,用了{}次就猜对了'.format(guesstimes))
- else:
- print('恭喜你,猜对了,用了{}次机会'.format(guesstimes))
- input('按回车退出。')
复制代码
我的问题:
第24句,return npc,的必要性是什么?为何要单独返回 npc的值?而不需要返回 nc 的值?
- import random as r
- def creatNums(): # 随机取数
- n=4 # 一共需要取4次
- numbers = [] # 设置一个空列表
- while n:
- number = str(r.randint(0,9)) # 随机取数
- while number in numbers: # 查重
- number = str(r.randint(0,9))
- numbers.append(number) # 把number放入列表
- n -= 1
- return (numbers) #以列表形式返回numbers
- def numjud(numbers, gnums): # 定义一个函数,用于评判(答案数字,猜测的数字)
- i = 0
- npc = 0 #全正确
- nc = 0 #数对,位置错
- for n in gnums: # 遍历猜测的列表
- if n in numbers: # 如果列表中,本数字存在
- nc += 1 # 数字猜对的值加一
- if numbers[i] == gnums[i]: # 如果列表中第0个数字值与位置与猜测值相同
- npc += 1 # 数字和位置均猜对,值加一
- nc -= 1 # 数字猜对的值,减回来一个
- i += 1 # 索引后移,i对应gnums内每个元素
- print('{}A{}B'.format(npc, nc)) #这里已经把题目中要求的结果打印了出来
- return npc #返回值是否必要,关键看调用部分
- #调用
- print('游戏开始,请你猜四个数字(0-9):')
- numbers = creatNums()
- npc = 0 #显然,这里是在为接受npc的返回值做准备
- guesstimes = 0
- while npc<4: #没有全猜对
- while True: #这个死循环是必要的,必须输入满足所有条件(进入内层if)才执行break,跳出来,否则,就算跳出,inputnum也不符合要求
- inputnums = input('请输入四个不同的数字:')
- if len(inputnums) == 4 and inputnums.isdigit(): #确定输入无误
- gnums = list(inputnums)
- if len(set(gnums)) == 4:
- # set() 函数,可创建无序不重复元素集
- break
- else:
- print('数字不能重复')
- else:
- print('输入有误!')
- gnums = list(inputnums)
- npc = numjud(numbers, gnums) #【*关键*】调用时,用npc接受返回值,后面没有再使用,所以就可有可无咯
- guesstimes += 1 #外层while循环结束条件为全猜对,否则,才一次,记录一次次数,下面是输出次数
- if guesstimes < 8:
- print('你真棒,用了{}次就猜对了'.format(guesstimes))
- else:
- print('恭喜你,猜对了,用了{}次机会'.format(guesstimes))
复制代码
|
|