扩大
缩小
人生本来就短暂,为什么还要栽培苦涩。
返回顶部

函数方式的注册,登录,可选择奖品的猜年龄游戏练习。

注册

#注册:
def func():
    count = 0
    while count < 3:
        username_inp = input("请输入你的用户名:")
        password_inp = input("请输入你的密码:")
        password_inp_new = input("请再次输入你的密码")
        if not password_inp == password_inp_new:
            print("两次密码输入不一致")
            count += 1
            continue
        with open("denglu.txt","a",encoding="utf-8") as fa:
            fa.write(f"{username_inp}:{password_inp}\n")
            fa.flush()
            break
func()

登陆

#登陆功能:
def Owen():
    username_inp = input("请输入你用户名")
    pwd_inp = input("请输入你的密码")
    with open("denglu.txt","r",encoding="utf-8") as fr:
        for denglu in fr:
            username,pwd = denglu.split(":")
            if username.strip() == username_inp and pwd.strip() == pwd_inp:
                print("登陆成功")
                break
        else:
            print("登陆失败")
Owen()

猜年龄

def cnl():
    """猜年龄"""
    age = 20
    count = 0
    while count < 3:
        age_inp = input("请输入你的年龄")
        if not age_inp.isdigit():
            print(f"请确定你的年龄{age_inp}")
            continue
        new_age_int = int(age_inp)
        if new_age_int > age:
            print("猜大了")
        elif new_age_int < age:
            print("猜小了")
        else:
            print("真棒,猜对了")
            break
    count += 1

cnl()

选择奖品函数

def prize():
    """获取奖品"""
    prize_dict = {'0':"绿巨人",'1':"美国队长","2":"巴雷特","3":"变形金刚"}
    prize_str = """如下奖品:
    0."绿巨人"
    1."美国队长"
    2."巴雷特"
    3."变形金钢"
    """
    count = 0
    prize_jp = {}
    while count < 2:
        print(f"奖品如下:{prize_str}")
        jp_prize = input("请输入你需要的奖品:")
        jp = prize_dict[jp_prize]
        if jp in jp_prize:
            prize_jp[jp] += 1
        else:
            prize_jp[jp] = 1
        print(f"恭喜你获得奖品:{jp}")
        count += 1
        print(f"一共获取多少个:{prize_jp}]")
prize()
posted @ 2019-09-19 19:31  晴天sky  阅读(128)  评论(0)    收藏  举报
左边日期 搜索的制定样式