标签:password 三次 实现 一个 logging 缩进 conf 习惯 nbsp
来个例子:
1 username=input("username:") 2 password=input("password:") 3 username1=‘whgvjp‘ 4 password1=‘0235‘ 5 if username==username1 and password==password1: 6 print(‘welcome user {name}logging in...‘.format(name=username)) 7 else: 8 print("The wrong username or password")
这里的话,需要注意在if语句和else语句后面要有冒号,接着会强制缩进,默认缩进单位是四个空格,如果不缩进会出错。Python这种风格可以使代码更美观,并且也能使程序员养成良好的写代码的习惯。
猜年龄的例子,这个例子可以实现只能猜三次,如果三次都猜不对,会打印一个“你已经尝试太多次..”。
count=1 age_of_oldboy=56 while count<4: 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 biger!") count+=1
else:
print("you have tried too mang times..")
也可以用for循环来实现:
count=0 age_of_oldboy=56 for count 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 biger!") count+=1 else: print(‘you have tried too many times..‘)
也可以count=1,然后for count in range(1,4):意义不大,都行。
下面这个代码跟上面的基本相同,但是可以实现,如果你在每猜三次后,不想继续猜了,那就输入’n‘退出这个猜年龄游戏;如果还想继续玩下去,可以输入除了‘n‘以外的任何一个键,继续游戏。
count=1 age_of_oldboy=56 for count in range (1,4): 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 biger!") count+=1 if count==4: countine_confirm=input(‘do you want to keep trying‘) if countine_confirm!=‘n‘: count=1
标签:password 三次 实现 一个 logging 缩进 conf 习惯 nbsp
原文地址:http://www.cnblogs.com/whgvjp/p/7565364.html