day1,

猜年龄,3次之后退出,
 1 #! /usr/bin/env python
 2 
 3 count = 0
 4 age_of_boy = 56
 5 
 6 while count < 3:
 7    guess_age = int(input("guess age:"))
 8    if guess_age == age_of_boy:
 9        print("you got it!")
10        break
11    elif guess_age > age_of_boy:
12        print("think bigger!")
13    else:
14        print("think smaller!")
15    count +=1
16 
17 else:
18   print("you have tried so many times!")
View Code

guess age:2
think smaller!
guess age:3
think smaller!
guess age:56
you got it!

 

改进版的猜年龄,不退出,重新猜,

 1 #! /usr/bin/env python
 2 
 3 count = 0
 4 age_of_boy = 56
 5 
 6 while count < 3:
 7 # for i in range(3):
 8    guess_age = int(input("guess age:"))
 9    if guess_age == age_of_boy:
10        print("you got it!")
11        break
12    elif guess_age > age_of_boy:
13        print("think bigger!")
14    else:
15        print("think smaller!")
16    count += 1
17    if count == 3:
18       confirm = input("do you want more guess?")
19       if confirm != "n":
20         count =0
View Code

 登陆接口,

输入用户名跟密码,认证成功显示欢迎信息,

最多尝试3次否则锁定账号。

 1 #! /usr/bin/env python
 2 
 3 login1 = "Welcome to our system!"
 4 login2 = "Pls inpur your name and password:"
 5 count = 0
 6 name = "mark"
 7 passwd = "123"
 8 
 9 while count < 3:
10     if count == 0:
11         print (login1)
12     else:
13         print (login2)
14     username = input("pls input your name:")
15     password = input("input your password:")
16 
17     if username == name and password == passwd:
18         print ("Welcome %s to our system " % username)
19         break
20     else:
21         print ("try it agin!")
22     count += 1
23 
24     if count == 3:
25         print ("You have been locked!")
View Code

 

用户交互,

#!/usr/bin/env python

name = input("your name is:")
age = int(input("your age is:"))
job = input("your job is:")
salary = input("your salary is:")



info = '''
--------info of %s--------------
Name:%s
Age:%d
Job:%s
Salary:%s
'''%(name,name,age,job,salary)


print(info)
View Code

your name is:mark

your age is:33

your job is:it

your salary is:5000

--------info of mark--------------

Name:mark

Age:33

Job:it

Salary:5000

 

#!/usr/bin/env python

name = input("your name is:")
age = int(input("your age is:"))
job = input("your job is:")
salary = input("your salary is:")



info2 = '''
--------info of{_name}--------------
Name:{_name}
Age:{_age}
Job:{_job}
Salary:{_salary}
'''.format(_name=name,_age=age,_job=job,_salary=salary)


print(info2)
View Code

 

密文,

import getpass

name = input("username:")
password = getpass.getpass("password:")

print (name,password)
View Code

 continue 跳出本次循环继续到下一次循环,break直接跳出循环,

posted @ 2017-04-10 10:58  寻_找北极星  阅读(170)  评论(0)    收藏  举报