2.7 练习1-猜数字游戏

#猜数字游戏
import random
# answer_sum=random.randint(1,10)
answer_sum=8
num_count=0  #游戏次数控制
login_count=0 #登录次数控制
prize_dict={ #设置奖品
    0:'cup',
    1:'cap',
    2:'pen',
    3:'notebook'
}

get_prize_dict=dict() #记录获奖情况

user_info_dict = {
    'coco':'123',
    'mike':'123'

}


while login_count<4:
    login_count += 1

    if login_count==4:
        print('账号密码已输入错误3次,系统关闭')
        break

    user_name = input('请输入你的用户名>>').strip()
    if user_name not in user_info_dict.keys():
        print('用户名输入错误')
        continue

    pwd = input('请输入你的用户密码>>').strip()
    if user_info_dict[user_name]!=pwd:
        print('账号密码错误')
        continue

    print('登录成功!欢迎来到猜数字游戏!!!')

    # 可以猜测三轮
    while num_count < 3:
        inp_sum = input('提示:范围为1-10\n请输入你猜测的数字>>').strip() #去除输入字符空格

        # 如果输入不是数字字符则进入下一轮循环
        if not inp_sum.isdigit():
            print('请输入正确的数字!!')
            continue

        inp_sum = int(inp_sum) #数字字符型转换为整型

        #回答正确
        if inp_sum==answer_sum:
            print('恭喜你回答正确!')
            print(prize_dict)

            #猜对可以选择两次奖品
            for i in range(2):
                prize_choice=input("请选择你的奖品,如果不想要输入'N'or 'n'退出>>").strip()

                if prize_choice!='N' or prize_choice!='n':
                    prize=prize_dict[int(prize_choice)]
                    print(f'恭喜你获得奖品:{prize}')

                    #记录奖品信息
                    if get_prize_dict.get(prize):
                        get_prize_dict[prize]+=1

                    else:
                        get_prize_dict[prize]=1
                else:
                    break

            login_count=4
            print(f'你已经获取奖品:{get_prize_dict}')
            break

        #输入答案偏大
        elif inp_sum>answer_sum:
            print('猜大了!')

        #输入答案偏小
        else:
            print('猜小了!')

        num_count+=1 #计数器+1
        num_count+=1 #计数器+1

        if num_count!=3:
            continue #不等于3下面代码不会执行,进入下一轮循环

        again_choice=input(f'您已经猜测三次,本次答案是{answer_sum}请输入"Y" or "y"继续游戏,或任意键退出>>') #由用户选择是否继续

        if again_choice=='y' or again_choice=='Y':
            num_count=0
        else:
            login_count=4
posted @ 2025-08-14 11:19  bokebanla  阅读(9)  评论(0)    收藏  举报