python制作的简单的猜数小游戏

今天花了一点时间用python写了个猜数小游戏,10分钟就能搞定!

废话不多说,直接上代码

由于代码太过于简单,所以没有什么注释。

import random

print("欢迎来到猜数小游戏!")
print("作者:Debug618\n")

cnt = 0
total_cnt = 0
while 1:
    tot = 0
    if cnt == 0:
        tot = int(input("1.开始游戏\n2.结束游戏\n"))
        while tot != 1 and tot != 2:
            tot = int(input("1.开始游戏\n2.结束游戏\n"))
    else:
        tot = int(input("1.继续游戏\n2.结束游戏\n"))
        while tot != 1 and tot != 2:
            tot = int(input("1.继续游戏\n2.结束游戏\n"))
    if int(tot) == 2:
        break
    l, r = input("请输入数的范围(两个数,用逗号隔开):").split(",")
    num = random.randint(int(l), int(r)) # 求出随机数
    your = int(input("请输入你猜的数:"))
    cai = 0
    if your == num:
        print("你太厉害了吧!");
        cai += 1
    while 1:
    	# 判断是否猜对,并给出提示
        if your < num:
            print("数太小了!")
            your = int(input("请输入你猜的数:"))
            cai += 1
        elif your > num:
            print("数太大了!")
            your = int(input("请输入你猜的数:"))
            cai += 1
        if your == num:
            print("恭喜你答对了!")
            print("你猜了" + str(cai) + "次!\n")
            total_cnt += cai
            break
    cnt += 1
print("---统计---\n你玩了" + str(cnt) + "局\n你一共猜了" + str(total_cnt) + "次\n")

今天的代码就是这些了,整体代码量还是很少的,也非常简单,新手也能写

那么我们下次再见!

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值