Python learning - (Part 2: Guess the number game)

Table of contents

1. After the previous study, I copied a small game of guessing numbers.

2, the code is as follows:

3. The demo results are as follows:

4. According to the demonstration results, the above code segment can perfectly solve the above requirements.


1. After the previous study, I copied a small game of guessing numbers.

       The problem is described as follows: the user randomly enters a number and compares it with a random number generated by the game. If they are the same, jump out of the loop and the game ends. If it is greater or smaller than the random number generated by the system, give the user a hint, similar to big or small. class, a total of three opportunities.

2, the code is as follows:

"""用python设计第一个小游戏"""
import random
answer = random.randint(1,10)
count = 3

while count > 0:
    temp = input("不妨猜一下小浪此时心里的数字:")
    guess = int(temp)

    if guess == answer:
        print("你是小甲鱼")
        print("亨,猜中了也没有奖励")
        break
    else:
        if guess < answer:
            print("小啦")
        else:
            print("大了")
    count = count - 1

print("游戏结束")

3. The demo results are as follows:

不妨猜一下小浪此时心里的数字:3
小啦
不妨猜一下小浪此时心里的数字:2
小啦
不妨猜一下小浪此时心里的数字:9
大了
猜错了,游戏结束
>>> 
======================== RESTART: B:/Desktop/game.py ========================
不妨猜一下小浪此时心里的数字:5
小啦
不妨猜一下小浪此时心里的数字:8
你是小甲鱼
亨,猜中了也没有奖励
猜错了,游戏结束
>>> 
======================== RESTART: B:/Desktop/game.py ========================
不妨猜一下小浪此时心里的数字:3
你是小甲鱼
亨,猜中了也没有奖励
游戏结束
>>> 5
5

4. According to the demonstration results, the above code segment can perfectly solve the above requirements.

Originality is not easy, please indicate the source for reprinting, thinks!

Guess you like

Origin blog.csdn.net/h1998040218/article/details/129169849