Python game - guess random number

The main principle of the random number guessing game is to use random to randomly generate random numbers within the range you want to specify, and use the if-else statement to define the judgment statement. After guessing wrong, it will prompt whether the guess is too big or too small.

import random
sjs = random.randint(1,100)
while True:
    shu= int(input("请输入你的随机数:"))
    if shu > sjs:
        print("你猜大了")
    elif shu < sjs:
        print("你猜小了")
    else:
        print("恭喜你,猜对了")
        break

 If you want to limit the number of guesses, you need to add a limit

import random
sjs = random.randint(1,100)
count = 0
while True:
    shu= int(input("请输入你的随机数:"))
    if shu > sjs:
        print("你猜大了")
    elif shu < sjs:
        print("你猜小了")
    else:
        print("恭喜你,猜对了")
        break
     if count = 6 
        print("您未猜中,游戏结束")
        break

The number of times is limited to 6, and the game ends if no guess is made

Guess you like

Origin blog.csdn.net/qq_52351946/article/details/130882873