第5次全天课笔记-20180812
第5次全天课笔记 0812
习题1:
设定一个用户名和密码,用户输入正确的用户名和密码,则显示登录成功,否则提示登录失败
用户最多失败3次,否则退出程序。
user_name_in_system = "root" user_passwd_in_system = "root123" for i in range(3): user_name_input = input("please input username:") user_passwd_input = input("please input passwd:") if user_name_input == user_name_in_system and \ user_passwd_input == user_passwd_in_system: print("login successfully!") break else: print("wrong username or password!") else: print("input times is used out! bye!")
user_name_in_system = "root" user_passwd_in_system = "root123" for i in range(3): user_name_input = input("please input username:") user_passwd_input = input("please input passwd:") if user_name_input == user_name_in_system and \ user_passwd_input == user_passwd_in_system: print("login successfully!") break else: print("wrong username or password!") if i ==2: print("input times is used out! bye!")
习题2:
自己实现一个函数,在一句话中查找某个单词的算法,存在返回索引号,否则返回False
sentence = "I am good" 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,"good"))
如果有多个:
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): if sentence[i:i+word_length] == word: position_list.append(i) return position_list print(find_word_index(sentence,"good"))
不用切片的方法:
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"))
练习len的用法:
>>> len("123")
3
>>> len([1,2,3])
3
>>> len((1,2,3))
3
>>> len({1:1,2:2})
2
>>> len(123)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: object of type 'int' has no len()
>>>
>>>
>>> len(str(123))
练习题:
随机生成一个整数,1-100之间
你最多猜5次,如果猜大了,提示大了
小了,提示小了,
猜对了,提示猜中。
5次都没猜中,就猜没猜中。
import random target_num = random.randint(1,100) for i in range(5): user_input_num = int(input("请输入你猜的数字:")) if target_num == user_input_num: print("你猜中了,数字是:",user_input_num) print("你猜了 %s 次" %i+1) break elif target_num > user_input_num: print("你猜小了") else: print("你猜大了") if i == 4: print("5次的机会用光了!Bye~")
什么时候用while,什么时候用for
当你要执行的次数不确定的时候,用while;
执行的次数明确,推荐用for。
While 条件:
XXX
什么时候为False:
0,””,[],(),{}
小练习:
使用while,计算随机数之和,超过100的时候,停止程序。
随机数1-20的范围产生,要求记录一下产生的随机数,以及
最后的和,以及随机数的个数。
import random result =0 random_num_list =[] while 1: random_num = random.randint(1,20) random_num_list.append(random_num ) result+=random_num if result >100: break
print("一共产生了 %s 个随机数:" %len(random_num_list))
print("产生随机数如下:",random_num_list)
print("最后的随机数之和:",result)
小题目:
遍历一个字符串
遍历一个列表
str = "hhfsdfhsudfhusd" for i in str: print(i) for i in range(len(str)): print(str[i]) list1 = [1,2,3,3,0,4,4,4,4] for i in list1: print(i) for i in range(len(list1)): print(list1[i])
做一下题:遍历一个列表中的嵌套列表和元组的所有元素,
将1-12的数字进行输出!
[[[1,2,3],4,5],7,8,(9,10,(11,12))]
l = [[[1,2,3],4,5],7,8,(9,10,(11,12))] for item in l: if isinstance(item,(list,tuple)): for i in item: if isinstance(i,(list,tuple)): for j in i: print (j) else: print(i) else: print (item)
素数—质数:只能被1和本身所整除的数,计算1-100之间的素数之和。
import math def is_prime(num): if not isinstance(num,int): return False if num == 1: return False if num == 2: return True for i in range(2,int(math.sqrt(num)+1)): if num%i == 0: return False return True
result = 0 for i in range(1,101): if is_prime(i): result+=i print (i) print(result)
小练习:
1 使用 for 的方式,求一下奇数之和
result = 0 for i in range(1,101): if i % 2 == 1: result += i print (result)
2 用输入多个数字,当输入偶数的时候求和,
输入奇数,不求和,输入.的时候结束求和,
打印求和结果
result = 0 while 1: input_data = input("请输入数字:") if input_data == ".": break else: if int(input_data) % 2 ==0: result+=int(input_data) print(result)
方法1:数字和10取余,判断是否大于0并且小于等于5
for i in range(10,51): if i %10 >0 and i % 10 <=5: print (i)
方法2:将数字转换为str,取各位的字符
判断字符是否在1-5内。
for i in range(10,51): if str(i)[1] in "12345": print (i)
嵌套的方式:
for i in "1234": for j in "12345": print(int(i+j))
判断是否可迭代:
#coding=utf-8
from collections import Iterable
print (isinstance('abc', Iterable)) # str是否可迭代
print (isinstance([1,2,3], Iterable)) # list是否可迭代
print (isinstance(123, Iterable))# 整数是否可迭代
print(isinstance(set([1,2,3]),Iterable)) #set是否可迭代
退出循环的方法1:
try: for i in range(5): for j in range(5): if i==3 and j ==3: raise print (i) except: pass
方法2:函数的实现return
def fun(): for i in range(5): for j in range(5): print(i,j) if i==3 and j ==3: return True fun()
方法3:
for i in range(5): for j in range(5): for k in range(5): if i == j == k == 3: break else: print (i, '----', j, '----', k) else: continue break else: continue break
练习题
1. 输入3个数字,达到3个数字求和,结束程序
result = 0 for i in range(3): result+=int(input("请输入一个数字:")) print(result)
2 用户输入不同的数据,当输入的数据达到3个数字的
时候,求和结束程序。(数字可以是整数)
提示:判断是否整数的方法,isdigit()
遍历所有的输入数据,判断是否在0-9的字符串范围内
def isdigit(s): for i in s: if i not in "0123456789": return False return True
result = 0 times = 0 while 1: input_data = input("请输入数据:") if isdigit(input_data): result+=int(input_data) times+=1 if times ==3: break print (result)
def isdigit(data): try: float(data) return True except: return False
print(isdigit("1"))
print(isdigit("1.99999999"))
print(isdigit("1/3"))
print(isdigit("5c"))
print(isdigit(""))
用嵌套列表的方式,输出一个矩阵
1 2 3
4 5 6
7 8 9
方法1:
l=[[1,2,3],[4,5,6],[7,8,9]] for i in l: for j in i: print(j,end=" ") print("")
方法2:
for i in range(len(l)): for j in range(len(l[i])): print (l[i][j],end = " ") print("")
求正反对角线之和
result =0 for i in range(len(l)): for j in range(len(l[i])): if i ==j : result += l[i][j]
result =0 for i in range(len(l)): for j in range(len(l[i])): if i +j==2 : result += l[i][j]
求最边上的值之和
l = [[1,2,3,4,5],[1,2,3,4,5],[1,2,3,4,5],[1,2,3,4,5],[1,2,3,4,5]] result = 0 for i in range(len(l)): for j in range(len(l[i])): if i == 0 or i == 4: result += l[i][j] else: if j ==0 or j ==4: result += l[i][j] print(result)