if_else_while_for

 import getpass
#标准库里要加密密码需要导包getpass.但是getpass在pycharm中不好用,需要在命令窗口中输入才管用. _username = "abc"
_password = "abc123"
username = input("username:")
# password = getpass.getpass("password:")
password = input("password:") if _username==username and _password==password:
print("Welcome user {name} login...".format(name=username))
else:
print("invalid username or password")
 age_of_oldboy = 56

 guess_age = int(input("guess age:"))

 if guess_age==age_of_oldboy:
print("yes, you got it.")
elif guess_age>age_of_oldboy:
print("think smaller")
else:
print("think bigger")
 age_of_oldboy = 56

 count=0
while True:
if count==3:
break
# print("count:",count)
guess_age = int(input("guess age:")) if guess_age==age_of_oldboy:
print("yes, you got it.")
break
elif guess_age>age_of_oldboy:
print("think smaller")
else:
print("think bigger")
count=count+1 #count+=1
if count==3:
print("you have tried too many times! Fuck off!") # 优化后的代码
while count<3:
guess_age = int(input("guess age:")) if guess_age==age_of_oldboy:
print("yes, you got it.")
break
elif guess_age>age_of_oldboy:
print("think smaller")
else:
print("think bigger")
count=count+1 #count+=1
else:
print("you have tried too many times! Fuck off!")
 '''
while循环实现
age_of_oldboy = 56 count=0
while True:
if count==3:
break
# print("count:",count)
guess_age = int(input("guess age:")) if guess_age==age_of_oldboy:
print("yes, you got it.")
break
elif guess_age>age_of_oldboy:
print("think smaller")
else:
print("think bigger")
count=count+1 #count+=1
if count==3:
print("you have tried too many times! Fuck off!") # 优化后的代码
while count<3:
guess_age = int(input("guess age:")) if guess_age==age_of_oldboy:
print("yes, you got it.")
break
elif guess_age>age_of_oldboy:
print("think smaller")
else:
print("think bigger")
count=count+1 #count+=1
else:
print("you have tried too many times! Fuck off!") '''
'''
for i in range(10): #range10其实是从0到9.
print("loop", i)
''' # for循环来实现
age_of_oldboy = 56
for i in range(3):
guess_age = int(input("guess age:"))
if guess_age == age_of_oldboy:
print("yes, you got it")
break
elif guess_age > age_of_oldboy:
print("think smaller")
else:
print("think bigger")
else:
print("you have tried too many times.. fuck off!")
 # 0到9的数字,隔一个打印一个到控制台
for i in range(0,10,2): #括号中的2是步长,不写的话默认是1.若想隔两个打印一uqw,则步长设置为3即可.即for i in range(0,10,3)
print("loop", i)
 # 每错三次不直接跳出,而是问玩家是否要继续
age_of_oldboy = 56 count=0
while True:
if count==3:
break
# print("count:",count)
guess_age = int(input("guess age:")) if guess_age==age_of_oldboy:
print("yes, you got it.")
break
elif guess_age>age_of_oldboy:
print("think smaller")
else:
print("think bigger")
count=count+1 #count+=1
if count==3:
continue_confirm = input('do you want to continue?')
if continue_confirm !='n':
count=0
else:
print("you have tried too many times! Fuck off!")
 # continue的用法: 跳出本次循环,继续下一次循环
for i in range(0,10):
if i<5:
print("loop", i)
else:
continue #跳出本次循环,继续下一次循环
print("hehe...")
 #break:结束当前循环
for i in range(10):
print("-----------------",i)
for j in range(10):
print(j)
if j>5:
break
上一篇:由于开发需求需要在附件查看页面添加水印,于是网上看到一位大牛写了一个js加水印的方法觉得很实用,也很方便,记录一下,哈哈


下一篇:codeforces 665D Simple Subset