您好,登录后才能下订单哦!
本篇文章给大家分享的是有关使用python怎么制作一个猜数字游戏,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。
Python是一种编程语言,内置了许多有效的工具,Python几乎无所不能,该语言通俗易懂、容易入门、功能强大,在许多领域中都有广泛的应用,例如最热门的大数据分析,人工智能,Web开发等。
import random import time
我们用变量与input实现
name = str(input('请输入用户名:')) print('欢迎您,'+name)
这里我们得用except制作乱输文本就游戏结束的程序
乱输文本就结束
try: age = int(input('请输入年龄:')) except ValueError: print('非法输入') age = 30000
顺便把年龄设为30000[滑稽]
再根据年龄大小分配金币
if age <10: gold = 500 print('您的初始金币为500') if age <20 and age >10: gold = 1000 print('您的初始金币为1000') if age >20 and age <30: gold = 1500 print('您的初始金币为1500') if age >30 and age <1000: gold = 200 print('年龄过大,您的初始金币为200')
用if语句保证金币小于10000,如果等于10000,游戏结束
if gold !=10000: Game_start = str(input('是否开始游戏?(True或False)')) while gold !=10000:
接下来就是最重要的游戏部分了,认真!
我们还得用if语句判断Game_start变量为True,并创建一个随机数列表
if Game_start =='True': list_123 = [random.randint(1,6),random.randint(1,6),random.randint(1,6),] while list_123 ==10: list_123 = [random.randint(1,6),random.randint(1,6),random.randint(1,6),]
然后设定回答变量和正确答案与错误答案
answer = str(input('请猜数(big或small):')) if list_123[0] + list_123[1] + list_123[2] >10: result = 'big' error_result = 'small' if list_123[0] + list_123[1] + list_123[2] <10: result = 'small' error_result = 'big'
还得用if语句判断正确、错误与非法输入的答案
if answer ==result: print('您赢了!') gold = gold + 100 print('随机数为'+str(list_123)) print('您现在有'+str(gold)+'金币') elif answer ==error_result: print('您输了...') print('随机数为'+str(list_123)) gold = gold - 100 print('您现在有'+str(gold)+'金币') else: print('非法输入') gold = 10000
再制作购买道具的模块
太长了我懒得写一句一句解析
if gold ==2000 or gold ==3000 or gold ==4000 or gold ==5000 or gold ==6000 or gold ==7000 or gold ==8000 or gold ==9000: answer = str(input('您现在可以购买道具,是否购买?(True或False):')) if answer =='True': print('请说出要购买der道具') print('*'*41) print('*金币翻倍器[现有的] 金币翻倍器[得到的]*') print('* 2000G 1500G *') print('*'*41) answer = str(input('请说出要购买der道具(金币翻倍器[现有的]说1,金币翻倍器[得到的]说2):')) if gold >2000 and answer =='1': gold = gold - 2000 answer = int(input('请说出要翻der倍数:')) gold = str(gold * answer) print('您现在有'+gold+'金币') gold = int(gold) elif gold >1500 and answer =='2': gold = gold - 1500 answer = int(input('请说出要翻der倍数:')) gold_mang = 100 * answer gold = gold + gold_mang print('您现在有'+str(gold)+'金币') gold = int(gold) else: print('没有这个道具,游戏结束') gold = 10000 elif answer =='False': print('继续游戏') else: print('非法输入') gold = 10000
我写到一半发现没有把上面的桥接上,所以现在来桥接
import random import time name = str(input('请输入用户名:')) print('欢迎您,'+name) try: age = int(input('请输入年龄:')) except ValueError: print('非法输入') age = 30000 gold = 10000 if age <10: gold = 500 print('您的初始金币为500') if age <20 and age >10: gold = 1000 print('您的初始金币为1000') if age >20 and age <30: gold = 1500 print('您的初始金币为1500') if age >30 and age <1000: gold = 200 print('年龄过大,您的初始金币为200') if gold !=10000: Game_start = str(input('是否开始游戏?(True或False)')) while gold !=10000: if Game_start =='True': list_123 = [random.randint(1,6),random.randint(1,6),random.randint(1,6),] while list_123 ==10: list_123 = [random.randint(1,6),random.randint(1,6),random.randint(1,6),] answer = str(input('请猜数(big或small):')) if list_123[0] + list_123[1] + list_123[2] >10: result = 'big' error_result = 'small' if list_123[0] + list_123[1] + list_123[2] <10: result = 'small' error_result = 'big' if answer ==result: print('您赢了!') gold = gold + 100 print('随机数为'+str(list_123)) print('您现在有'+str(gold)+'金币') elif answer ==error_result: print('您输了...') print('随机数为'+str(list_123)) gold = gold - 100 print('您现在有'+str(gold)+'金币') else: print('非法输入') gold = 10000 if gold ==2000 or gold ==3000 or gold ==4000 or gold ==5000 or gold ==6000 or gold ==7000 or gold ==8000 or gold ==9000: answer = str(input('您现在可以购买道具,是否购买?(True或False):')) if answer =='True': print('请说出要购买der道具') print('*'*41) print('*金币翻倍器[现有的] 金币翻倍器[得到的]*') print('* 2000G 1500G *') print('*'*41) answer = str(input('请说出要购买der道具(金币翻倍器[现有的]说1,金币翻倍器[得到的]说2):')) if gold >2000 and answer =='1': gold = gold - 2000 answer = int(input('请说出要翻der倍数:')) gold = str(gold * answer) print('您现在有'+gold+'金币') gold = int(gold) elif gold >1500 and answer =='2': gold = gold - 1500 answer = int(input('请说出要翻der倍数:')) gold_mang = 100 * answer gold = gold + gold_mang print('您现在有'+str(gold)+'金币') gold = int(gold) else: print('没有这个道具,游戏结束') gold = 10000 elif answer =='False': print('继续游戏') else: print('非法输入') gold = 10000
其实到这里我们就基本完成了,但如果只写到这里会有很多bug,不想看了也可以就玩这个版本
还剩了一些代码,我也懒得写了,直接完整代码
import random import time name = str(input('请输入用户名:')) print('欢迎您,'+name) try: age = int(input('请输入年龄:')) except ValueError: print('非法输入') age = 30000 gold = 10000 if age <10: gold = 500 print('您的初始金币为500') if age <20 and age >10: gold = 1000 print('您的初始金币为1000') if age >20 and age <30: gold = 1500 print('您的初始金币为1500') if age >30 and age <1000: gold = 200 print('年龄过大,您的初始金币为200') if gold !=10000: Game_start = str(input('是否开始游戏?(True或False)')) while gold !=10000: if Game_start =='True': list_123 = [random.randint(1,6),random.randint(1,6),random.randint(1,6),] while list_123 ==10: list_123 = [random.randint(1,6),random.randint(1,6),random.randint(1,6),] answer = str(input('请猜数(big或small):')) if list_123[0] + list_123[1] + list_123[2] >10: result = 'big' error_result = 'small' if list_123[0] + list_123[1] + list_123[2] <10: result = 'small' error_result = 'big' if answer ==result: print('您赢了!') gold = gold + 100 print('随机数为'+str(list_123)) print('您现在有'+str(gold)+'金币') elif answer ==error_result: print('您输了...') print('随机数为'+str(list_123)) gold = gold - 100 print('您现在有'+str(gold)+'金币') else: print('非法输入') gold = 10000 if gold ==2000 or gold ==3000 or gold ==4000 or gold ==5000 or gold ==6000 or gold ==7000 or gold ==8000 or gold ==9000: answer = str(input('您现在可以购买道具,是否购买?(True或False):')) if answer =='True': print('请说出要购买der道具') print('*'*41) print('*金币翻倍器[现有的] 金币翻倍器[得到的]*') print('* 2000G 1500G *') print('*'*41) answer = str(input('请说出要购买der道具(金币翻倍器[现有的]说1,金币翻倍器[得到的]说2):')) if gold >2000 and answer =='1': gold = gold - 2000 answer = int(input('请说出要翻der倍数:')) gold = str(gold * answer) print('您现在有'+gold+'金币') gold = int(gold) elif gold >1500 and answer =='2': gold = gold - 1500 answer = int(input('请说出要翻der倍数:')) gold_mang = 100 * answer gold = gold + gold_mang print('您现在有'+str(gold)+'金币') gold = int(gold) else: print('没有这个道具,游戏结束') gold = 10000 elif answer =='False': print('继续游戏') else: print('非法输入') gold = 10000 if gold ==0: print('金币没了,游戏结束') gold = 10000 elif Game_start =='False': print('欢迎游玩,下次再见!') gold = 10000 else: print('非法输入') gold = 10000 print('游戏结束,再见!') time.sleep(10)
以上就是使用python怎么制作一个猜数字游戏,小编相信有部分知识点可能是我们日常工作会见到或用到的。希望你能通过这篇文章学到更多知识。更多详情敬请关注亿速云行业资讯频道。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。