1 import getpass 2 #标准库里要加密密码需要导包getpass.但是getpass在pycharm中不好用,需要在命令窗口中输入才管用. 3 4 _username = "abc" 5 _password = "abc123" 6 username = input("username:") 7 # password = getpass.getpass("password:") 8 password = input("password:") 9 10 if _username==username and _password==password: 11 print("Welcome user {name} login...".format(name=username)) 12 else: 13 print("invalid username or password")
1 age_of_oldboy = 56 2 3 guess_age = int(input("guess age:")) 4 5 if guess_age==age_of_oldboy: 6 print("yes, you got it.") 7 elif guess_age>age_of_oldboy: 8 print("think smaller") 9 else: 10 print("think bigger")
1 age_of_oldboy = 56 2 3 count=0 4 while True: 5 if count==3: 6 break 7 # print("count:",count) 8 guess_age = int(input("guess age:")) 9 10 if guess_age==age_of_oldboy: 11 print("yes, you got it.") 12 break 13 elif guess_age>age_of_oldboy: 14 print("think smaller") 15 else: 16 print("think bigger") 17 count=count+1 #count+=1 18 if count==3: 19 print("you have tried too many times! Fuck off!") 20 21 22 # 优化后的代码 23 while count<3: 24 guess_age = int(input("guess age:")) 25 26 if guess_age==age_of_oldboy: 27 print("yes, you got it.") 28 break 29 elif guess_age>age_of_oldboy: 30 print("think smaller") 31 else: 32 print("think bigger") 33 count=count+1 #count+=1 34 else: 35 print("you have tried too many times! Fuck off!")
1 ''' 2 while循环实现 3 age_of_oldboy = 56 4 5 count=0 6 while True: 7 if count==3: 8 break 9 # print("count:",count) 10 guess_age = int(input("guess age:")) 11 12 if guess_age==age_of_oldboy: 13 print("yes, you got it.") 14 break 15 elif guess_age>age_of_oldboy: 16 print("think smaller") 17 else: 18 print("think bigger") 19 count=count+1 #count+=1 20 if count==3: 21 print("you have tried too many times! Fuck off!") 22 23 24 # 优化后的代码 25 while count<3: 26 guess_age = int(input("guess age:")) 27 28 if guess_age==age_of_oldboy: 29 print("yes, you got it.") 30 break 31 elif guess_age>age_of_oldboy: 32 print("think smaller") 33 else: 34 print("think bigger") 35 count=count+1 #count+=1 36 else: 37 print("you have tried too many times! Fuck off!") 38 39 ''' 40 ''' 41 for i in range(10): #range10其实是从0到9. 42 print("loop", i) 43 ''' 44 45 # for循环来实现 46 age_of_oldboy = 56 47 for i in range(3): 48 guess_age = int(input("guess age:")) 49 if guess_age == age_of_oldboy: 50 print("yes, you got it") 51 break 52 elif guess_age > age_of_oldboy: 53 print("think smaller") 54 else: 55 print("think bigger") 56 else: 57 print("you have tried too many times.. fuck off!")
1 # 0到9的数字,隔一个打印一个到控制台 2 for i in range(0,10,2): #括号中的2是步长,不写的话默认是1.若想隔两个打印一uqw,则步长设置为3即可.即for i in range(0,10,3) 3 print("loop", i)
1 # 每错三次不直接跳出,而是问玩家是否要继续 2 age_of_oldboy = 56 3 4 count=0 5 while True: 6 if count==3: 7 break 8 # print("count:",count) 9 guess_age = int(input("guess age:")) 10 11 if guess_age==age_of_oldboy: 12 print("yes, you got it.") 13 break 14 elif guess_age>age_of_oldboy: 15 print("think smaller") 16 else: 17 print("think bigger") 18 count=count+1 #count+=1 19 if count==3: 20 continue_confirm = input('do you want to continue?') 21 if continue_confirm !='n': 22 count=0 23 else: 24 print("you have tried too many times! Fuck off!")
1 # continue的用法: 跳出本次循环,继续下一次循环 2 for i in range(0,10): 3 if i<5: 4 print("loop", i) 5 else: 6 continue #跳出本次循环,继续下一次循环 7 print("hehe...")
1 #break:结束当前循环 2 for i in range(10): 3 print("-----------------",i) 4 for j in range(10): 5 print(j) 6 if j>5: 7 break