逻辑练练手小程序
程序员文章站
2022-04-28 14:13:40
写个练练手,自己增加要求增加写,至上而下写 ......
写个练练手,自己增加要求增加写,至上而下写
# 小程序要求: # 1.用户可能输入内容中前后多按了空格进行去除 # 2.用户不按规定输入内容返还输入,并给与警告提示 # 3.需要记录用户的信息,输入次数规定3 # 4.提示密码错误,账号错误,或者两者都错 # 4.奖品可选猜对了可以获取2个 # 5.猜的机会有3次 # 6.拿了奖品还能选择能否继续玩 # 7.年龄范围随机 18-15 # 导入随机函数 import random # 需要用到的信息 gift_dict = { '1': 'altman', '2': 'cat', '3': 'dog', '4': 'pig' } username_pwd_dict = { 'yangwenyi': 123, 'nick': 123 } user_dict = dict() # 计数 gift_count = 0 age_count = 0 username_count = 0 # 规定年龄 under_age = random.randrange(18, 26) # 随机数字18-25 # 用户交互 输入账号密码 while username_count < 3: user_name = input('请输入你的名字:').strip() pwd = int(input('请输入你的密码').strip()) name_list = username_pwd_dict.keys() if pwd == username_pwd_dict.get(user_name): print(f'欢迎{user_name}来到猜年龄游戏') username_count = 4 elif user_name in name_list: print('账号正确,密码错误') username_count += 1 else: username_count += 1 print('账号错误') if username_count == 1: print('你已经输错了一次账号密码了还剩2次机会') elif username_count == 2: print('你已经错两次了,' '就剩下1次机会了') elif username_count == 3: print('机会已经用完了') user_name_choose = input('叫爸爸在给你次机会').strip() if user_name_choose == '爸爸': username_count = 0 else: print('大丈夫能屈能伸,' '你居然这也办不到太让我失望了游戏结束') # 用户交互猜年龄 print('骚年我们开始猜年龄吧年龄18到25,' '猜对了有礼物哦,你有三次机会') while age_count < 3: age_input = input('请输入你的年龄').strip() if not age_input.isdigit(): print('好好输入数字,' '猜对了后面有礼物给你哦') age_count += 1 print(f'你已经浪费了{age_count}次机会了,' f'还剩{3 - age_count}次机会') continue else: if int(age_input) == under_age: print('骚年猜对了,来选礼物吧') age_count = 4 gift_count = 0 elif int(age_input) > under_age: print('大了') age_count += 1 print(f'已经使用了{age_count}次机会了,' f'还剩{3 - age_count}次机会') else: print('小了') age_count += 1 print(f'已经使用了{age_count}次机会了,' f'还剩{3 - age_count}次机会') # 与用户交互猜对了拿里面,兼是否还玩 while age_count == 4 and gift_count != 2: gift_chose = input('输入1奖品altma,' '输入2奖品catt,' '输入3奖品为dog,' '输入4奖品为pig\n' '请选择:').strip() if not gift_dict.get(gift_chose): gift_count += 1 print(f'你个*这也输入错了,礼物较少{gift_count}个') else: gift_count += 1 print(f'已经选择{gift_dict[gift_chose]},还有{2 - gift_count}选礼物机会') if gift_dict[gift_chose] not in user_dict: user_dict[gift_dict[gift_chose]] = 1 else: user_dict[gift_dict[gift_chose]] += 1 if gift_count == 2: print(f"恭喜你获得\n'altman'{user_dict.get('altman',0)}个\n" f"'cat'{user_dict.get('cat',0)}个\n" f"'dog'{user_dict.get('dog', 0)}个\n" f"'pig'{user_dict.get('pig', 0)}个") #get函数没找到返回,0 age_count = 3 # 与用户交互要不要继续 while age_count == 3: age_chose = input('输入小哥哥我们接着游戏,输入我不玩了结束游戏\n' '请输入') if age_chose == '小哥哥': age_count = 0 elif age_chose == '我不玩了': age_count = 100 print('拜拜') else: print('好好输入')
上一篇: 字符串,列表,元组
下一篇: Python的集合与字典练习