Python猜字游戏(用函数)

#coding=utf-8

#猜字游戏

#导入随机数
import random


#判断数的大小与答案的大小
def guess_judge(num):
    while (True):
        guess_judge_num = get_input_num()
        if (guess_judge_num == num):
            print("太棒了,答对了")
            break
        elif (guess_judge_num > num):
            print("比答案大,再猜")
        else:
            print("比答案小,再猜")


#获取输入的数字
def get_input_num():
    while (True):
        a = input("请输入一个0到10的数:")
        try:
            guess_judge_num = int(a)
        except:
            print("输入不合法,请重新输入")
            continue
        if (guess_judge_num <0 or guess_judge_num >10):
            print("请重新输入一个0到10的数")
            continue
        else:
            break
    return guess_judge_num


#开始或者退出游戏
print("猜数游戏开始啦")
while (True):
    num = random.randint(0, 10)
    guess_judge(num)
    str = input("请输入R继续下一轮游戏,输入其他退出")
    if(str == "R"):
        print("新一轮游戏开始,请准备")
    else:
        print("游戏退出")
        break

参考:https://ke.qq.com/course/456373?taid=3946752822933173

上一篇:A 1146 Topological Order (25分)(拓扑排序)


下一篇:并查集例题02.带权并查集(poj1182)