【原创】分享一个小游戏

帖主用Python做了一个猜数字游戏,刚学Python不久,只用到了几个常见的代码,欢迎大家游玩这个游戏!有什么BNG欢迎交流!

import random
import time

p = ((random.randint(1,10) , 5 , '(1-10)') , (random.randint(1,50) , 10 , '(1-50)') , (random.randint(1,100) , 15 , '(1-100)') , (random.randint(1,500) , 20 , '(1-500)') , (random.randint(1,1000) , 25 , '(1-1000)'))
print('欢迎来玩猜数字游戏 , 请选择模式 .')
print('( 如果一次猜中请去买彩票 )')
while True:
    try:
        time.sleep(1)
        print('''\n\n1. 1~10
2. 1~50
3. 1~100
4. 1~500
5. 1~1000
6. 结束''')
        time.sleep(1)
        print('''
----------【请选择模式】----------''')
        s = input('>> ') . strip()
        if s:
            x = int(s)
            if x > 0 and x < 7:
                if x == 6:
                    print('游戏结束')
                    time.sleep(5)
                    break
                else:
                    k , t , m = p[x - 1][0] , p[x - 1][1] , p[x - 1][2]
                    while t:
                        print('您有 %d 次机会 : ' % t)
                        time.sleep(1)
                        print('----------【猜】----------')
                        c = int(input('%s >> ' % m))
                        if c == k:
                            print()
                            print('哇 , 厉害呀')
                            print('祝贺,您猜对了!')
                            p = ((random.randint(1,10) , 5 , '(1-10)') , (random.randint(1,50) , 10 , '(1-50)') , (random.randint(1,100) , 15 , '(1-100)') , (random.randint(1,500) , 20 , '(1-500)') , (random.randint(1,1000) , 25 , '(1-1000)'))
                            if t == p[x - 1][1]:
                                time.sleep(1)
                                print('强烈建议去买彩票!!!')
                            print()
                            break
                        else:
                            s = ''
                            d = c - k if c > k else k - c
                            e = '+' if c > k else '-'
                            while d:
                                s = s + e
                                d //= 10
                            print('哥, 大了大了 ' + s) if c > k else print('哥, 小了小了 ' + s)
                            t -= 1
                    else:
                        time.sleep(0.5)
                        print('很遗憾,次数已经用完!')
        else:
            print('游戏结束')
            time.sleep(5)
            break
    except:
        print('请输入数字1~6!!!')

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值