Python基本练习题
程序员文章站
2022-05-11 09:48:17
1、打印1-100的数 for i in range(1, 101): print(i) 2、打印1+2+..100的总和 num = 1 sum = 0 while num < 101: sum += num num += 1 print(sum) 3、使用while输出1 2 3 4 6 i = ......
1、打印1-100的数
for i in range(1, 101): print(i)
2、打印1+2+..100的总和
num = 1 sum = 0 while num < 101: sum += num num += 1 print(sum)
3、使用while输出1 2 3 4 6
i = 1 while i < 7: if i == 5: print('') else: print(i) i += 1
4、输出1-100内所有奇数
for i in range(1, 101): if i % 2 != 0: print(i)
5、用户验证登录
username = 'liangxiao' password = '123..com' username_in = input('请输出您的名字:') password_in = input('请输出您的密码:') num = 1 while num < 4: num += 1 if username_in == username and password_in == password: print('登录成功!欢迎您的回归!') break else: print('账号密码不匹配,请确认后重新输入!') num = 4 - num num = '您当前还剩余输入次数机会%d' % (num) print(num) username_in = input('请输出您的名字:') password_in = input('请输出您的密码:')
6、格式化输出
name = input('请输出你的名字:') age = input('请输出你的年龄:') information = ''' 你的个人信息如下: 你的名字:%s 你的年龄:%s ''' % (name, age) print(information)
7、python的基础运算
6 or 2 > 1 = 6 3 or 2 > 1 = 3 0 or 5 < 4 = false 5 < 4 or 3 = 3 2 > 1 or 6 = true 3 and 2 > 1 = true 0 and 3 > 1 = false 2 > 1 and 3 = 3 3 > 1 and 0 = 0 1 > 2 or 4 < 7 and 8 == 8 3 > 1 and 2 or 2 < 3 and 3 and 4 or 3 > 2 = 2 not 2 > 1 and 3 < 4 or 4 > 5 and 2 > 1 and 9 > 8 or 7 < 6 = false
8、计算 1 - 2 + 3 - 4 ... + 99
num = 0 sum = 0 while num < 100: num += 1 if num == 88: continue elif num % 2 != 0: sum += num else: sum -= num print(sum)
9、设置用户输入内容检测
input1 = input('请输出您需要注册的密码:') sensitive_words = ['小粉锤', '大铁锤'] i = 0 while i < 3: if input1 in sensitive_words: print('您输出的内容有误,请重新输入!') input1 = input('请输出您需要注册的密码:') else: print('输出正确,登录成功!') print('欢迎您的回归!') break i += 1
10、验证码简单验证
code = 'ac8e' code_input = input('请输出验证码:') if code_input.upper() == code: print('验证通过') else: print('验证失败')
11、实现一个列表用户添加
li = [] username = input('>>>请输出您需要添加的用户:') while true: if username.upper() != 'q': li.append(username) print('添加成功') operation = input('是否继续?(y/n)') if operation.upper() == 'y': username = input('>>>请输出您需要添加的用户:') elif operation.upper() == 'n': print('welcome to use this program!') break else: print('您的输入有误!') username = input('>>>请输出您需要添加的用户:') else: print('welcome to use this program!') break
12、 列表[11 ,22...99];大于66的加入到key2中,小于66的放入到key1中
list1 = [11, 22, 33, 44, 55, 66, 77, 88, 99] dict1 = {'key1':'', 'key2':''} list2 = [] list3 = [] for i in list1: if i > 66: list2.append(i) else: list3.append(i) dict1['key2'] = list2 dict1['key1'] = list3 print(dict1)
13、输出商品列表,用户输入序号,显示用户选中的商品
商品 list1 = ["手机", "电脑", '鼠标垫', '游艇'],要求如下:
1:页面显示 序号 + 商品名称,如:
1 手机
2 电脑
3 鼠标垫
4 游艇
2: 用户输入选择的商品序号,然后打印商品名称
3:如果用户输入的商品序号有误,则提示输入有误,并重新输入。
4:用户输入q或者q,退出程序。"""
list1 = ['手机', '电脑', '鼠标垫', '游艇'] for i in list1: print('{}\t{}'.format(list1.index(i)+1,i)) while 1: choose = input('请输出您的选择:(1-4)') if choose.isdigit(): if int(choose) > 0 and int(choose) < 5: print('输入正确!') print(list1[int(choose)-1]) break else: print('您输入的序列有误,请重新输入!') continue elif choose.isalpha(): if choose.upper() == 'q': print('good-bye!') break else: print('退出失败,您的输出有误') continue else: print('您的输入有误,请重新输入!')
14、账号注册登录验证
while 1: username = input('请输出您需要注册的账号:') password = input('请输出您需要注册的密码:') if username.isalnum() and password.isalnum(): print('可以注册√') username_new = input('请再次确认您需要注册的账号:') password_new = input('请再次确认您需要注册的密码:') if username_new == username and password_new == password: print('注册成功!') break else: input('注册信息有误,请重新输入\n') username_file = open('username', mode='a', encoding='utf-8') password_file = open('password', mode='a', encoding='utf-8') username_file.write(username) password_file.write(password) username_file.close() password_file.close() for i in range(3): login_username = input('请登录用户名:') login_password = input('请登录密码:') username = open('username', mode='r', encoding='utf-8') password = open('password', mode='r', encoding='utf-8') read_user = username.read() read_pass = password.read() if login_username == read_user and login_password == read_pass: print('登录成功!') break else: ii = i + 1 num = 3 - ii print('登录失败,请重新登录!您还剩余{}次数'.format(num)) else: print('账号已锁定!')
上一篇: 匿名管道通讯实现