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,通过更新前一部的练习题让自己也学到了不少 ...
随机推荐
- lower_bounder()和upper_bound()的函数
lower_bound() .upper_bound()都运用于有序区间的二分查找. ForwardIter lower_bound(ForwardIter first, ForwardIter la ...
- Linux下的python安装
centos7安装python3 以及tab补全功能 1.安装python3 1.1下载python源码包 网址:https://www.python.org/downloads/release/ ...
- Python学习之路2☞数据类型与变量
变量 变量作用:保存状态:说白了,程序运行的状态就是状态的变化,变量是用来保存状态的,变量值的不断变化就产生了运行程序的最终输出结果 一:声明变量 #!/usr/bin/env python # -* ...
- 洛谷2387 BZOJ3669魔法森林题解
题目链接 BZ链接 这道题被很多人用spfa水了过去,表示很... 其实spfa很好卡,这组数据可以卡掉大多数spfa 链接:密码:rjvk 这里讲一下LCT的做法 我们按照a将边排序,然后依次添加 ...
- hdu 1054 【树形dp】
http://acm.hdu.edu.cn/showproblem.php?pid=1054 给定一棵树,点能看住与其相连的边,问最少需要选定多少个点看住所有的边. 定义dp[maxn][2],dp[ ...
- 基于TableStore的海量气象格点数据解决方案实战
前言 气象数据是一类典型的大数据,具有数据量大.时效性高.数据种类丰富等特点.气象数据中大量的数据是时空数据,记录了时间和空间范围内各个点的各个物理量的观测量或者模拟量,每天产生的数据量常在几十TB到 ...
- PHP 7.0新增特性详解
https://www.cnblogs.com/riverdubu/archive/2017/03/22/6434705.html 开始介绍PHP7.0新特性,具体的可以参照官网的介绍,我来挑一些给大 ...
- HTML标题title滚动
上代码: <script type="text/javascript"> var msg = document.title; msg = "…" + ...
- LightOJ 1269 Consecutive Sum (Trie树)
Jan's LightOJ :: Problem 1269 - Consecutive Sum 题意是,求给定序列的中,子序列最大最小的抑或和. 做法就是用一棵Trie树,记录数的每一位是0还是1.查 ...
- poj 1066 Treasure Hunt (Geometry + BFS)
1066 -- Treasure Hunt 题意是,在一个金字塔中有一个宝藏,金字塔里面有很多的墙,要穿过墙壁才能进入到宝藏所在的地方.可是因为某些原因,只能在两个墙壁的交点连线的中点穿过墙壁.问最少 ...