Python入门代码练习
一、循环猜年龄程序,猜错三次则打印提示信息并退出循环,猜对也打印提示信息并退出循环
count=0
while count < 3:
num = input("猜年龄游戏:")
guess = int(num)
if guess == 18:
print("恭喜你猜对了")
break
elif guess > 18:
print("大了")
elif guess < 18:
print("小了")
count += 1
else:
print("猜了三次都没猜对'")
二、写while循环嵌套的小例子,用一个tag控制所有while循环的退出
tag=True
while tag:
inp_name=input("name>>")
inp_pwd=input('password>>')
if inp_name == "username" and inp_pwd == "password":
print('login successfull')
while tag:
cmd=input('cmd>>>')
if cmd == 'quit':
tag=False
continue
print('%s 命令整在执行...' %cmd)
else:
print('user or password not vaild')
三、用户注册程序
基本功能实现一
ps:使用手机号码做账户
menu=['新用户注册','查看账户信息','退出程序']
while True:
for i in range(len(menu)): #打印功能menu菜单
print(i, menu[i])
choice=input("选择编号>>>:").strip()
if choice =='': #判断用户选择的功能是'新用户注册'
number=input('请输入你的手机号码>>>:').strip()
uname=input('请输入用户名>>>:').strip()
passwd=input('请输入密码>>>:').strip()
age=input('请输入你的年龄>>>:').strip()
sex=input('请输入你的性别>>>:').strip()
with open('db.txt',mode='a',encoding='utf-8') as f: #创建一个存在用户信息的文件
f.write('%s,%s,%s,%s,%s\n' %(number,uname,passwd,age,sex)) #把用户输入的注册信息写入文件中
elif choice == '': #判断用户选择的功能是'查看账户信息'
number=input('请输入你的手机号码>>>:').strip() #让用户输入手机号码
with open('db.txt',mode='r',encoding='utf-8') as f:
for i in f:
if i.startswith(number): #判断用户输入的账号是否存在
print(i,end='') #打印该账号详细信息
break #退出此循环
elif choice == '': #功能2'退出程序'
print('退出程序')
break
else: #以上条件不满足,让用户信息输入!
print('输入非法数据,请重新输入!!!')
功能实现二
#查看用户信息,增加密码认证功能
menu=['用户注册','查看账户信息','退出程序']
tag=True
while True:
for i in range(len(menu)):
print(i, menu[i])
choice=input("选择编号>>>:").strip()
if choice =='':
print('用户注册')
number=input('请输入你的手机号码>>>:').strip()
uname=input('请输入用户名>>>:').strip()
passwd=input('请输入密码>>>:').strip()
age=input('请输入你的年龄>>>:').strip()
sex=input('请输入你的性别>>>:').strip()
with open('db.txt',mode='a',encoding='utf-8') as f:
f.write('%s,%s,%s,%s,%s\n' %(number,uname,passwd,age,sex))
elif choice == '':
tag=True
while tag:
number=input('请输入你的手机号码>>>:').strip()
with open('db.txt',mode='r',encoding='utf-8') as f:
for i in f:
if i.startswith(number):
while tag:
check=i.split(',')
uname=input('请输入用户名>>>:').strip()
passwd=input('请输入密码>>>:').strip()
if uname == check[] and passwd == check[]: #判断用户密码,用户密码输入不正确时,让用户重新输入
print(check)
tag=False
else:
print('账号密码错误!!!')
elif choice == '':
print('退出程序')
break
else:
print('输入非法数据,请重新输入!!!')
功能实现三
ps:增加账号是否已被注册功能
menu=['用户注册','查看账户信息','退出程序']
tag=True
while True:
for i in range(len(menu)):
print(i, menu[i])
choice=input("选择编号>>>:").strip()
if choice =='':
while tag:
number=input('请输入你的手机号码>>>:').strip()
with open('db.txt',mode='r',encoding='utf-8') as f: #注意:使用r模式打开db.txt,db.txt文件不存在会报错
for i in f:
if i.startswith(number):
print('该账号已被注册,请重新输入!!!')
break
uname=input('请输入用户名>>>:').strip()
passwd=input('请输入密码>>>:').strip()
age=input('请输入你的年龄>>>:').strip()
sex=input('请输入你的性别>>>:').strip()
with open('db.txt',mode='a',encoding='utf-8') as f:
f.write('%s,%s,%s,%s,%s\n' %(number,uname,passwd,age,sex))
break
elif choice == '':
tag=True
while tag:
number=input('请输入你的手机号码>>>:').strip()
with open('db.txt',mode='r',encoding='utf-8') as f:
for i in f:
if i.startswith(number):
while tag:
check=i.split(',')
uname=input('请输入用户名>>>:').strip()
passwd=input('请输入密码>>>:').strip()
if uname == check[] and passwd == check[]:
print(check)
tag=False
else:
print('账号密码错误!!!')
elif choice == '':
print('退出程序')
break
else:
print('输入非法数据,请重新输入!!!')
四、购物车程序
用户名和密码存放于文件中,格式为:aaa|bbb123
启动程序后,先登录,登录成功则让用户输入工资,然后打印商品列表,失败则重新登录,超过三次则退出程序
允许用户根据商品编号购买商品
用户选择商品后,检测余额是否够,够就直接扣款,不够就提醒
可随时退出,退出时,打印已购买商品和余额
menu=['新用户注册','登录商城','退出程序']
goods=[
['mac',],
['lenovo',],
['apple',],
['tesla',],
] shopping_cart=[ ] tag=True
while True:
for i in range(len(menu)):
print(i, menu[i])
choice=input("选择编号: ").strip()
if choice == '':
while tag:
user_number=input('请输入你的手机号码: ').strip()
with open(r'db.txt',mode='r',encoding='utf-8') as read_db_f: #模式r模式打开,db.txt文件不存在会报错
for i in read_db_f:
if len(user_number) != or user_number in i: #文件内容为空时会让重复输入手机号码,文件数据类型为 12345678901|18|hello|male
print('该账户已被注册,请重新输入一个11号码!')
break
else:
passwd=input('请输入密码: ').strip()
age=input('请输入你的年龄: ').strip()
sex=input('请输入你的性别: ').strip()
with open('db.txt',mode='a',encoding='utf-8') as f:
f.write('%s|%s|%s|%s\n' %(user_number,passwd,age,sex))
tag=False
break
elif choice == '':
tag=True
while tag:
user_number=input('请输入你的手机号码: ').strip()
with open('db.txt',mode='r',encoding='utf-8') as f:
for i in f:
if i.startswith(user_number):
check=i.split('|')
passwd=input('请输入密码: ').strip()
if user_number == check[] and passwd == check[]:
price=
print('当前账户余额: ',price)
while tag:
salary=input('余额不足请充值: ')
if not salary.isdigit():
print('输入非法数据,请重新输入!!!')
continue
salary=int(salary)
price=salary+price
print('充值成功,当前余额为:',price)
for i in range(len(goods)):
print(i,goods[i])
while tag:
number=input('根据商品编号购买该商品: ')
if number == '':
if price >= goods[][]:
prcie=price-goods[][]
print('当前余额为: ',price)
shopping_cart.append('%s' %(goods[]))
else:
print('余额不足,无法购买此商品,请先充值!')
break
elif number == '':
if price >= goods[][]:
price=price-goods[][]
print('当前余额:',price)
shopping_cart.append('%s' %(goods[]))
else:
print('余额不足,无法购买此商品,请先充值!')
break
elif number == '':
if price >= goods[][]:
price=price-goods[][]
print('当前余额: ',price)
shopping_cart.append('%s' %(goods[]))
else:
print('余额不足,无法购买此商品,请先充值!')
break
elif number == '':
if price >= goods[][]:
price=price-goods[][]
print('当前余额: ',price)
shopping_cart.append('%s' %(goods[]))
else:
print('余额不足,无法购买此商品,请先充值!')
break
elif number == 'quit':
print('已购买商品:',shopping_cart)
tag=False
else:
print('商品不存在,请重新输入!')
else:
print('账号密码错误!!!')
elif choice == '':
print('退出程序')
break
else:
print('输入非法数据,请重新输入!!!')
五、三级菜单
打印省、市、县三级菜单
可返回上一级
可随时退出程序
menu = {
'北京':{
'海淀':{
'五道口':{
'soho':{},
'网易':{},
'google':{}
},
'中关村':{
'爱奇艺':{},
'汽车之家':{},
'youku':{},
},
'上地':{
'百度':{},
},
},
'昌平':{
'沙河':{
'老男孩':{},
'北航':{},
},
'天通苑':{},
'回龙观':{},
},
'朝阳':{},
'东城':{},
},
'上海':{
'闵行':{
"人民广场":{
'炸鸡店':{}
}
},
'闸北':{
'火车战':{
'携程':{}
}
},
'浦东':{},
},
'山东':{},
}
tag=True
while tag:
for k in menu:
print(k)
choice = input('请选择>>>: ').strip()
if choice in menu:
while tag:
for k2 in menu[choice]:
print(k2)
choice2 = input('请选择>>>: ').strip()
if choice2 in menu[choice]:
while tag:
for k3 in menu[choice][choice2]:
print(k3)
choice3 = input('请选择>>>: ').strip()
if choice3 in menu[choice][choice2]:
for k4 in menu[choice][choice2][choice3]:
print(k4)
choice4 = input('请选择>>>: ').strip()
if choice4 == 'b':
break
elif choice4 == 'quit':
tag=False
elif choice3 == 'b':
break
elif choice3 == 'quit':
tag=False
elif choice2 == 'b':
break
elif choice2 == 'quit':
tag=False
elif choice == 'quit':
tag=False
六、打印九九乘法表
'''
*= #layer= 运算次数1
*= *= #layer= 运算次数2
*= *= *=
...
*
'''
for layer in range(,):
# # print('第%d层' %layer)
for j in range(,layer+):
print('%s*%s=%s ' %(layer,j,layer*j),end='')
print()
七、打印金字塔
'''
#max_layer=
* #current_layer=,space=,star=
*** #current_layer=,space=,star=
***** #current_layer=,space=,star=
******* #current_layer=,space=,star=
********* #current_layer=,space=,star= space=max_layer - current_layer
star=*current_layer-
''' max_layer=
for current_layer in range(,max_layer+):
# print(current_layer)
# 打印空格
for i in range(max_layer - current_layer):
print(' ',end='') # 打印星号
for j in range(*current_layer-):
print('*',end='') print()
七、登录接口
基础需求:
- 让用户输入用户名和密码
- 认证成功后显示欢迎信息
- 输错三次后退出程序并锁定账户
userinfo={
'egon':{'password':'','count':},
'alex':{'password':'','count':},
'wupeiqi':{'password':'','count':},
}
lock_file=[]
count =
while True:
username=input('请输入用户名:').strip()
password=input('请输入密码:').strip()
if username not in userinfo:
print('用户名不存在!')
continue
elif username in lock_file:
print('用户被锁定,请联系管理员!')
print(lock_file)
break
elif userinfo[username]['count'] > :
print('密码输入错误超过三次,%s被锁定,请联系管理员' %username)
print(lock_file)
lock_file.append(username)
elif password == userinfo[username]['password']:
print('登录成功')
break
else:
print('密码错误')
userinfo[username]['count']+=
Python入门代码练习的更多相关文章
- 移动测试之appium+python 入门代码(四)
最近工作中想要做自动化回归测试,想法是将每个测试用例都做自动截图,然后将最近的稳定版本和当前测试的版本的两张截图去对比,也要将两个版本的截图都放到测试报告中方便人工来进行验证.最初想法是通过HTMLT ...
- 移动测试之appium+python 入门代码(二)
ps: 对于环境安装可能会碰到各种问题,还是要一一解决. 执行: appium-doctor 显示上边界面说明,环境已完成. 同时将手机连接主机(用数据线) ^_^ 执行 adb devices 显示 ...
- 移动测试之appium+python 入门代码(三)
在做app自动化过程中会踩很多坑,咱们都是用中文的app,所以首先要解决中文输入的问题!本篇通过屏蔽软键盘,绕过手机的软键盘方法,解决中文输入问题. 一.定位搜索 1.打开淘宝点击搜索按钮,进入搜索页 ...
- python入门(5)使用文件编辑器编写代码并保存执行
python入门(5)使用文件编辑器编写代码并保存执行 两款文本编辑器: 一个是Sublime Text,免费使用,但是不付费会弹出提示框: 一个是Notepad++,免费使用,有中文界面: 请注意, ...
- 2018-06-21 中文代码示例视频演示Python入门教程第五章 数据结构
知乎原链 续前作: 中文代码示例视频演示Python入门教程第四章 控制流 对应在线文档: 5. Data Structures 这一章起初还是采取了尽量与原例程相近的汉化方式, 但有些语义较偏(如T ...
- 2018-06-20 中文代码示例视频演示Python入门教程第四章 控制流
知乎原链 续前作: 中文代码示例视频演示Python入门教程第三章 简介Python 对应在线文档: 4. More Control Flow Tools 录制中出了不少岔子. 另外, 输入法确实是一 ...
- 2018-06-20 中文代码示例视频演示Python入门教程第三章 简介Python
知乎原链 Python 3.6.5官方入门教程中示例代码汉化后演示 对应在线文档: 3. An Informal Introduction to Python 不知如何合集, 请指教. 中文代码示例P ...
- Python 入门之代码块、小数据池 与 深浅拷贝
Python 入门之代码块.小数据池 与 深浅拷贝 1.代码块 (1)一个py文件,一个函数,一个模块,终端中的每一行都是代码块 (代码块是防止我们频繁的开空间降低效率设计的,当我们定一个变量需要开辟 ...
- python入门简介
Python前世今生 python的创始人为吉多·范罗苏姆(Guido van Rossum).1989年的圣诞节期间,吉多·范罗苏姆为了在阿姆斯特丹打发时间,决心开发一个新的脚本解释程序,作为ABC ...
随机推荐
- CSA Round #54 $\ $Voting
CSA Round #54 \(\ \)Voting 题目大意: 原题网址:戳我戳我! 一次歌唱比赛中,一位歌手刚刚结束表演,评委正在打分. 一共有 \(n\) 位评委,他们每人可以打 \(1\) 分 ...
- 剑指offer-(19)顺时针打印矩阵
题目描述 输入一个矩阵,按照从外向里以顺时针的顺序依次打印出每一个数字,例如,如果输入如下矩阵: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 则依次打印出数字1,2, ...
- centos7安装eclipse方法
很多喜欢编程的朋友并不是很喜欢使用Windows来编写程序,尽管可视化编程但是操作相对繁琐,因而只在电脑上装有Linux系统,那么我们来说一下Linux下安装Java EE编程工具eclipse的方法 ...
- 9.C++-对象的构造函数(详解)
大家都定义struct或class时,不能给成员直接赋值,那么对象中成员变量的初始值是多少? 对于局部对象变量而言,其成员是个随机值,因为该变量是被分配在栈上,对于其它局部变量也是这样. 对于全局对象 ...
- 解决IAR printf函数输出中文字符乱码问题
首先看一下IAR的中文字符的坑 这会对调试造成很大的干扰,因为眼见不一定为实. 你所期望的中文打印输出都成了乱码,心在滴血.... 解决方法详细,纯属个人摸索 1.新建notepad++文件,编码方式 ...
- 回顾JS Date()对象
突然想写一个日历插件发现Date对象的一些常识快忘光了,复习一下 new Date()返回当前时间 年月日 getFullYear() 返回年份 getMonth() 返回月份(因为从0开始算 所以要 ...
- WordPress后台添加友情链接管理功能
其实很早之前WordPress是有这个功能的,但是伴随着wordpress的经常升级和主题的升级以及更换,有时候后台会发现没有链接管理的入口,不过还是可以通过代码还原这个功能. 将以下代码添加到您当前 ...
- 简述TCP的三次握手过程
一.TCP报文格式 TCP/IP协议的详细信息参看<TCP/IP协议详解>三卷本.下面是TCP报文格式图: 图1 TCP报文格式 上图中有几个字段需要重点介绍下: ...
- 走进webpack(1)--环境拆分及模块化
初级的文章和demo已经基本完成了,代码也已经上传到了我的github上,如果你对webpack的使用并不是十分了解,那么建议你回头看下走近系列,里面包括了当前项目中使用频繁的插件,loader的讲解 ...
- 常用Markdown公式整理 && 页内跳转注意 && Markdown preview
目录: 常用Markdown公式及注意事项 标题 列表 链接 区块 代码块 / 引用 粗体和斜体 文字块 图片 表格 横线 页内跳转注意事项 其他重要需注意 Markdown preview 前提: ...