课后题:
0)请谈下你对列表的理解
1)什么情况下我们要是循环永远为真?
答:判断输入的数是否符合某种类型等等
flag = True
longStr = 0
#判断是否含有 *
while flag:
temp = input("请输入密码:")
for i in temp:
if i == "*":
print("密码中不能含有“*”号!您还有 3 次机会!",end="")
break
else:
longStr += 1
if longStr == len(temp):
flag = False
#三次输入
while num > 0:
if temp == passWord:
print("密码正确,进入程序......")
break
elif temp != passWord:
num -= 1
if num == 0:
print("你已经失去输入密码机会")
break
print("你还有",end="");print(num,end="");print("次机会,请输入密码:")
temp = input()
print("100~999的所有水仙花数")
for i in range(100,999):
if i == ((i%1000-i%100)/100)**3+((i%100-i%10)/10)**3+(i%10)**3:
print(i)
2)
print("红球 3 个,黄球 3 个,绿球 6 个,随机抽出 8 个,所有组合:")
for red in range(4):
for yellow in range(4):
for green in range(2,7):
if red+green+yellow == 8:
print(red,yellow,green)
收获小结:
0)判断字符串中是否有某个字符串,可以使用资格运算符 in
>>> if "h" in "huanghaozhuoyuan":
print(True)
True
1)要注意如何是自己的代码运行更高效
如:
i = 0
string = 'ILoveFishC.com'
while i < len(string)):
print(i)
i += 1
改后:不需要每次都计算字符串长度,而是一次算出
i = 0
string = 'ILoveFishC.com'
length = len(string)
while i < length:
print(i)
i += 1
0)请谈下你对列表的理解
1)什么情况下我们要是循环永远为真?
答:判断输入的数是否符合某种类型等等
编程题
0)
passWord = "everybody"
num = 3flag = True
longStr = 0
#判断是否含有 *
while flag:
temp = input("请输入密码:")
for i in temp:
if i == "*":
print("密码中不能含有“*”号!您还有 3 次机会!",end="")
break
else:
longStr += 1
if longStr == len(temp):
flag = False
#三次输入
while num > 0:
if temp == passWord:
print("密码正确,进入程序......")
break
elif temp != passWord:
num -= 1
if num == 0:
print("你已经失去输入密码机会")
break
print("你还有",end="");print(num,end="");print("次机会,请输入密码:")
temp = input()
1)
print("100~999的所有水仙花数")
for i in range(100,999):
if i == ((i%1000-i%100)/100)**3+((i%100-i%10)/10)**3+(i%10)**3:
print(i)
2)
print("红球 3 个,黄球 3 个,绿球 6 个,随机抽出 8 个,所有组合:")
for red in range(4):
for yellow in range(4):
for green in range(2,7):
if red+green+yellow == 8:
print(red,yellow,green)
收获小结:
0)判断字符串中是否有某个字符串,可以使用资格运算符 in
>>> if "h" in "huanghaozhuoyuan":
print(True)
True
1)要注意如何是自己的代码运行更高效
如:
i = 0
string = 'ILoveFishC.com'
while i < len(string)):
print(i)
i += 1
改后:不需要每次都计算字符串长度,而是一次算出
i = 0
string = 'ILoveFishC.com'
length = len(string)
while i < length:
print(i)
i += 1