python.day1:赋值 / 格式化输出 / 逻辑判断 / 循环

声明字符集:  #一般2.x版本使用

# -*- coding:utf-8 -*-

 

赋值:

Name = input("Name:")
Age = int(input("Age:"))
print(type(Age),type(str(Age)))    #  格式化转换 
Job = input("job:")
Salary = input("salary:")

 

 

格式话输出:

(官方建议使用)

info2='''
 --- info2 of {_name} ----
 Name : {_name}
 Age : {_age}
 Job : {_job}
 Salary : {_salary}
'''.format(_name=Name,
            _age=Age,
            _job=Job,
            _salary=Salary
           )
print(info2)

 

逻辑判断:

 

if...else:

import getpass
_username="sunyw"
_password="abc123"
username=input("username:")
#password=input("password:")
password=getpass.getpass("password:")
if _username == username and _password == password:
    print("welcome to {name} login..".format(name=username))
else:
    print("invlid username or password")

 

if...elif...else:

age_of_sunyw = 56
guss_age = int(input("guss_age:"))
if age_of_sunyw == guss_age:
    print("you get it!")
elif age_of_sunyw > guss_age:
    print("think bigger..")
else:
    print("think smaller...")

 

 

循环:

while:

ge_of_sunyw = 20
count = 0
while count < 3:
    guss_age = int(input("gusse age:"))
    if age_of_sunyw == guss_age:
        print("you get it!")
        break
    elif age_of_sunyw < guss_age:
        print("think small..")
    else:
        print("think bigger..")
    count +=1
else:
    print("too many input to close!")

 

for:

age_of_sunyw = 20
for i in range(3):
    guss_age = int(input("gusse age:"))
    if age_of_sunyw == guss_age:
        print("you get it!")
        break
    elif age_of_sunyw < guss_age:
        print("think small..")
    else:
        print("think bigger..")
else:
    print("too many times to close!")

 

 

练习1:3次用户名密码不对就退出

import getpass
_username = "sunyw"
_password = "abc123"
count = 2
while count > -1:
    username = input("name:")
    password = getpass.getpass("password:")
    if _password == password and _username == username:
        print("****welcome {name} login..".format(name=username))
        break
    else:
        print("***name or password wrong!you have {_count} times".format(_count=count))
        count -=1
else:
    print("---over!---")

 

练习2: 任意猜,猜3次以后系统提示,是否继续,n退出 ,其他键继续

old_os_sunyw = 20
count = 0
while count < 3:
    guess_age = int(input("guess age:"))
    if guess_age == old_os_sunyw:
        print("you get it!")
        break
    elif guess_age < old_os_sunyw:
        print("---think bigger..")
    else:
        print("---think small..")
    count +=1
    if count == 3:
        continue_fire = input("---do you want to continue? enter 'n/N' to over  ")
        if continue_fire != 'n' and continue_fire != 'N':
            count = 0
else:
    print("---over---")

 

 

break #跳出

continue #继续

for i in range(0,20):
if i < 5:
print(i)
elif i > 5 and i < 15:
continue
else:
print(i)

 

posted @ 2018-07-16 10:18  月半月半儿瓜  阅读(163)  评论(0编辑  收藏  举报