python 猜数字游戏详解

 这个代码实现了手动输入猜测的次数 从而进行随机数的猜测

import random

print("这是一个猜数字游戏")
print("游戏开始!")
guess_frequency = int(input("请设定你的猜测次数:"))
random_numbers = random.randint(1,100)
frequency = 0
input_numbers = None
while input_numbers != random_numbers and frequency < guess_frequency:
    input_numbers = int(input("请输入一个数: "))
    if input_numbers < random_numbers:
        print("输入的数小了,请重新输入!")
    elif input_numbers > random_numbers:
        print("输入的数大了,请重新输入!")
    else:
        print("猜对了!")
    frequency += 1
if frequency == guess_frequency:
    print("你的{}次机会全部用完!".format(frequency))
else:
    print("你还有{}次机会!真棒!".format(guess_frequency-frequency))
print("游戏结束!")

其中

guess_frequency是你手动输入选择猜测的次数

frequency为你实际猜测的次数

random_numbers为解释器给定的随机数

input_numbers为你猜测的随机数

你可能感兴趣的:(python,游戏)