服务器之家

服务器之家 > 正文

python实战之90行代码写个猜数字游戏

时间:2021-10-18 09:46     来源/作者:啥也不是

一、导入库

?
1
2
import random
import time

二、注册用户

我们用变量与input实现

?
1
2
name = str(input('请输入用户名:'))
print('欢迎您,'+name)

三、注册年龄

这里我们得用except制作乱输文本就游戏结束的程序

乱输文本就结束

?
1
2
3
4
5
try:
    age = int(input('请输入年龄:'))
except ValueError:
    print('非法输入')
    age = 30000

顺便把年龄设为30000[滑稽]

再根据年龄大小分配金币

四、分配金币

?
1
2
3
4
5
6
7
8
9
10
11
12
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判断 1

?
1
2
3
if gold !=10000:
    Game_start = str(input('是否开始游戏?(True或False)'))
    while gold !=10000:

接下来就是最重要的游戏部分了,认真!

我们还得用if语句判断Game_start变量为True,并创建一个随机数列表

六、if与随机数

?
1
2
3
4
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),]

然后设定回答变量和正确答案与错误答案

七、回答、正确与错误

?
1
2
3
4
5
6
7
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判断 2

?
1
2
3
4
5
6
7
8
9
10
11
12
13
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

再制作购买道具的模块

太长了我懒得写一句一句解析

九、购买道具

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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

我写到一半发现没有把上面的桥接上,所以现在来桥接

十、桥接

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
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,不想看了也可以就玩这个版本

还剩了一些代码,我也懒得写了,直接完整代码

十一、完整代码

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
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实战之90行代码写个猜数字的文章就介绍到这了,更多相关python猜数字内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://blog.csdn.net/tanclyyy/article/details/115871982

标签:

相关文章

热门资讯

yue是什么意思 网络流行语yue了是什么梗
yue是什么意思 网络流行语yue了是什么梗 2020-10-11
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全 2019-12-26
背刺什么意思 网络词语背刺是什么梗
背刺什么意思 网络词语背刺是什么梗 2020-05-22
2021年耽改剧名单 2021要播出的59部耽改剧列表
2021年耽改剧名单 2021要播出的59部耽改剧列表 2021-03-05
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总 2020-11-13
返回顶部