filter()函数问题
来源:3-2 项目作业
火火火火火火火火火
2020-01-16 22:43:29
import sys import random from datetime import datetime #提示玩家进入游戏,并输出如效果图标题所示信息 def guide_page(guide_wold): print("{}{}{}".format("*"*30, guide_wold, "*"*30)) #自定义数字类型判断 def all_num(n): return n.isdigit() #自定义数值合法性判断 def num_legal(ls): ls1 = list(ls) if ls1[0] == ls1[1]: print("你输入的区间值相同!!请重新启动程序") sys.exit() elif ls1[0] > ls1[1]: print("你输入的数字区间大小有误,请重新启动程序") sys.exit() else: print("所产生的随机数字区间为:", ls) return 1 #自定义产生指定区间随机数 def set_final_num(num1, num2): num3 = [num1, num2] if all_num(num1) and all_num(num2) == True: num_legal(num3) return random.randint(int(num1), int(num2)) else: print("您输入的为非数字字符,请重新启动") sys.exit() def check_num_legal(num): if num not in range(int(i),int(j)+1): return True def write_record(times,value): with (open("record.txt", "a", encoding="utf_8")) as f: f.write("{}: 第{}次猜测的数字是:{}".format(datetime.now(), times, value)) f.write("\n") def main(rand1): temp = 0 while True: a = int(input("请继续输入你猜测的数字:")) if check_num_legal(a) == True: print("您所输入的数字未在指定区间,请重新输入:") temp +=1 write_record(temp, a) if a == rand1: print("恭喜你!只用了{}次就猜对了".format(temp)) break else: print("Lower than the answer\n""*"*10) return 1 if __name__ == "__main__": guide_page("欢迎进入数字猜猜猜小游戏") i = input("数字区间起始值:") j = input("数字区间终止值:") temp = set_final_num(i,j) main(temp)
总的能实行了,
但是,思路4:
filter()函数调用,无法调用,不明所以
2回答
同学你好:
同学的代码还有很多需要注意的地方。
1、区间的比较是数字类型间的比较,不是字符串的比较,因此需要转换为int类型。字符串之间的比较是先比较第一个字符ASCII值的大小,如果相同再比较字符串的长度。同学根据之前的代码输入2和10即可,2的ASCII值大于1的。
2、使用filter方法参考下面的使用,然后再判断lst和num3是否相同,相同说明输入的都是数字。
3、输入的数字不在猜测的范围。使用continue关键字跳出当前次的循环,进入下一次循环。
同学的条判断要判断完整,不要偷懒,猜测的数小于随机数,猜测的数大于随机数。
不要使用return关键字,由于同学之前没有使用continue关键字,因此当输入的数不在猜测的范围,依然会继续向下执行,会执行print("Lower than the answer\n""*"*10)后,执行return 1 。
结束函数的执行,并且函数的返回值为1,程序结束。
如果我解决了同学的问题,请采纳!学习愉快^_^。
火火火火火火火火火
提问者
2020-01-16
还有那个return 1没看明白,最后个是不小心带上了
相似问题