nickkkkkkk

  博客园 :: 首页 :: 新随笔 :: 联系 :: 订阅 :: 管理 ::
三种格式化输出
 
# Author:Alex Li
 
 
name = input("name:")
#raw_input 2.x   input 3.x
#input 2.x =
age = int(input("age:") ) #integer           类型转换
print(type(age)   , type(  str(age) ))
job = input("job:")
salary  = input("salary:")
 
info = '''
-------- info of  %s  -----
Name:%s
Age:%d     相当于占位符,d代表int,s代表字符串
Job:%s
Salary:%s
''' % (name,name,age,job,salary)
 
info2 = '''
-------- info of {_name}  -----
Name:{_name}
Age:{_age}
Job:{_job}
Salary:{_salary}
'''.format(_name=name,
           _age=age,
           _job=job,
           _salary=salary)
 
info3 =  '''
-------- info of {0} -----
Name:{0}
Age:{1}
Job:{2}
Salary:{3}
'''.format(name,age,job,salary)
print(info3)
 
 
 
判断用户登录时用户名和密码是否匹配,密文方式
# Author:Alex Li
import getpass      导入模块
 
_username = 'alex'
_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!")
 
 
indentationError,表示的时候缩进错误
 
 
while 循环
 
# Author:Alex Li
 
age_of_oldboy = 56
 
count = 0
这种语法还是比较方便的,C++里反正是没有
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 +=1
else:
    print("you have tried too many times..fuck off")
 
 
for 循环
 
# Author:Alex Li
'''
count = 0
while True:
    print("count:",count)
    count = count +1  #count +=1
    if count == 1000:
        break
'''
'''
for i in range(0,10):
    if i <3:
        print("loop ",i)
    else :
        continue                  continue  跳出当前这次的循环
    print("hehe...")
'''
 
for i in range(10):                   range(10):    0~10
    print('----------',i)
    for j in range(10):
        print(j)
        if j >5:
            break                       break 结束整个循环
 
 
 
 
 
while 循环优化,选择是否继续guess
 
 
age_of_oldboy = 56
 
count = 0
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 +=1
    if count == 3:
        countine_confirm = input("do you want to keep guessing..?")
        if countine_confirm != 'n':
            count =0
#else:
#    print("you have tried too many times..fuck off")
posted on 2018-04-25 10:45  nickkkkkkk  阅读(102)  评论(0)    收藏  举报