python 练习题2
# 习题1:
# 设定一个用户名和密码,用户输入正确的用户名和密码,
# 则显示登录成功,否则提示登录失败,用户最多失败3次,
# 否则退出程序。
username="test"
password="test1234"
for i in range(3):
user_name=input("input the username:")
pass_word=input("input the password")
if user_name==user_name and pass_word==password:
print("login successfully!")
break
else:
print("wrong username or password!")
else:
print("input time is used out!")
方法2:
username="test"
password="test1234"
for i in range(3):
user_name=input("input the username:")
pass_word=input("input the password")
if user_name==user_name and pass_word==password:
print("login successfully!")
break
else:
print("wrong username or password!")
if i==2:
print("input time is used out!")
习题2:
# 自己实现一个函数,在一句话中查找某个单词的算法,
# 存在返回索引号,否则返回False
#
# 提示:使用句子中的坐标遍历句子的每一个位置,
# 使用查找单词的长度结合使用切片来查找单词。
# 例如:s[i:i+len(单词)]
sentence="I am a boy."
def find_word_index(sentence,word):
word_length = len(word)
for i in range(len(sentence)-word_length+1):
if sentence[i:i+word_length] == word:
return i
return -1 print(find_word_index(sentence,"boy")) # 判断的标准,你找到的位置,单词开始和单词结束的位置,判断单词的开始是0或者它的前面不是字母,且结束诶之的后面不是字母或者是句子的最后一个位置、
# 方法二:判断一个完整的单词
sentence = "I am good good good boy!" def find_word_index(sentence,word):
position_list =[]
word_length = len(word)
for i in range(len(sentence)-word_length+1):
for j in range(word_length):
if sentence[i+j] != word[j]:
break
else:
position_list.append(i)
return position_list print(find_word_index(sentence,"good"))
# 练习题:
# 随机生成一个整数,1-100之间
# 你最多猜5次,如果猜大了,提示大了
# 小了,提示小了,
# 猜对了,提示猜中。
# 5次都没猜中,就猜没猜中。
import random i=random.randint(1,100)
print(i) for j in range(5):
a = int(input("input a number"))
if a==i:
print("guess it right")
print("use",j+1, "times")
break
elif a>i:
print("it is too big")
continue
elif a<i:
print("it is too small")
continue
else:
print("input time is used out ,game over") # 使用while,计算随机数之和,超过100的时候,停止程序。
# # 随机数1-20的范围产生,要求记录一下产生的随机数,以及
# # 最后的和,以及随机数的个数。 import random
s=[]
sum=0
while 1:
i =random.randint(0,20)
print(i)
s.append(i)
sum+=i
print(sum)
if sum>100:
break
print("一共产生了%s 个随机数"%len(s))
print(sum)
print(s) s="asdf"
for i in range(len(s)):
print("基于长度遍历",s[i])
for j in s:
print("基于字符串遍历",j)
python 练习题2的更多相关文章
- Python练习题 028:求3*3矩阵对角线数字之和
[Python练习题 028] 求一个3*3矩阵对角线元素之和 ----------------------------------------------------- 这题解倒是解出来了,但总觉得 ...
- Python练习题 027:对10个数字进行排序
[Python练习题 027] 对10个数字进行排序 --------------------------------------------- 这题没什么好说的,用 str.split(' ') 获 ...
- Python练习题 026:求100以内的素数
[Python练习题 026] 求100以内的素数. ------------------------------------------------- 奇怪,求解素数的题,之前不是做过了吗?难道是想 ...
- Python练习题 025:判断回文数
[Python练习题 025] 一个5位数,判断它是不是回文数.即12321是回文数,个位与万位相同,十位与千位相同. ---------------------------------------- ...
- Python练习题 024:求位数及逆序打印
[Python练习题 024] 给一个不多于5位的正整数,要求:一.求它是几位数,二.逆序打印出各位数字. ---------------------------------------------- ...
- Python练习题 004:判断某日期是该年的第几天
[Python练习题 004]输入某年某月某日,判断这一天是这一年的第几天? ---------------------------------------------- 这题竟然写了 28 行代码! ...
- Python练习题-1.使用匿名函数对1~1000求和,代码力求简洁。
Python 练习 标签(空格分隔): Python Python练习题 Python知识点 一.使用匿名函数对1~1000求和,代码力求简洁. 答案: In [1]: from functools ...
- PYTHON练习题 二. 使用random中的randint函数随机生成一个1~100之间的预设整数让用户键盘输入所猜的数。
Python 练习 标签: Python Python练习题 Python知识点 二. 使用random中的randint函数随机生成一个1~100之间的预设整数让用户键盘输入所猜的数,如果大于预设的 ...
- python 基础 2.8 python练习题
python 练习题: #/usr/bin/python #coding=utf-8 #@Time :2017/10/26 9:38 #@Auther :liuzhenchuan #@File ...
- Python练习题2
如果真的想学精,学什么都不是好学的,如果真的想把Python学的出神入化,几乎自己想做什么都可以,就要下定恒心,坚持下去. 接下来继续更新Python练习题2,通过更新前一部的练习题让自己也学到了不少 ...
随机推荐
- hdu4176 水题
#include<stdio.h> #include<string.h> #include<algorithm> using namespace std; #def ...
- ios开发――解决UICollectionView的cell间距与设置不符问题
在用UICollectionView展示数据时,有时我们希望将cell的间距调成一个我们想要的值,然后查API可以看到有这么一个属性: - (CGFloat)minimumInteritemSpaci ...
- JavaWeb登录、注销、退出、记住用户名和密码
应该是保存在Cookie里,session是放在服务器的内存里的.在用户关闭了网页窗口后,session就清空了.而Cookie是保存在用户的IE临时文件夹中的,再次登录时,读取其中的值传给服务器. ...
- 2019-6-11-WPF-如何在应用程序调试启动
title author date CreateTime categories WPF 如何在应用程序调试启动 lindexi 2019-06-11 09:32:35 +0800 2018-2-13 ...
- Jmeter监控
https://www.cnblogs.com/saryli/p/6596647.html JMeter是一款压力测试工具,我们也可以用它来监控服务器资源使用情况. JMeter正常自带可以通过Tom ...
- CF1054F Electric Scheme
CF1054F Electric Scheme 其实没啥的. 离散化后,每行每列选择一个. 但是可能会相交 每行或每列相邻两个点成为一小段. 小段按照行列左右部点 小段有交,连inf边,每个s-左, ...
- poj1741 树上距离小于等于k的对数 点分治 入门题
#include <iostream> #include <stdio.h> #include <string.h> #include <algorithm& ...
- 2019-8-31-PowerShell-通过-WMI-获取系统信息
title author date CreateTime categories PowerShell 通过 WMI 获取系统信息 lindexi 2019-08-31 16:55:58 +0800 2 ...
- php中 array_filter函数 的总结
1.用此函数来过滤数组中的空元素 $arr1 = array('a'=>1,'b'=>0,'c'=>'','d'=>null,'e'=>5,'f'=>false); ...
- hdu 3374 String Problem (字符串最小最大表示 + KMP求循环节)
Problem - 3374 KMP求循环节. http://www.cnblogs.com/wuyiqi/archive/2012/01/06/2314078.html 循环节推导的证明相当 ...