手记

python基础

1.输入输出。

用户输入:


name = input("username:")        #在py2里用raw_input输入,同input

passwd = input("password:")

age = int(input ("age:"))

输出1


print(type(age))    #输出变量类型,默认是str

print(name,passwd)


输出2


info1 = '''

------------info of  ---------------

username:%s

password:%s

age:%d

'''%(name,passwd,age)

print(info1)


输出2


info2 = '''

-----------info of {_name}-----------------

username:{_name}

password:{_password}

age:{_age}

'''.format(_name=name,

            _password=passwd,

            _age=age,

             )

print(info2)

2.getpass加密


import getpass

username = "fengxiaoli"

password = "123456"

name = input("username:")

passwd = getpass.getpass("password:")   #注getpass模块在pycharm执行不成功,可以在命令行执行测试

if name == username and passwd == password:

    print("welcome {_username} login".format(_username=name))

else:

    print("invalid login")

3.while,for循环


#猜数字1

age = 50

count=0

while count < 3:

    _age = int(input("age:"))

    if _age == age :

        print("you guessed right")

        break

    elif _age > age:

         print("The number is too big")

    else:

         print ("The number is too small")

    count +=1

else :

    print("You tried too many times")


猜数字2

age = 50

for i in range(3):

    _age = int(input("age:"))

    if _age == age :

        print("you guessed right")

        break

    elif _age > age:

         print("The number is too big")

    else:

         print ("The number is too small")

else :

    print("You tried too many times")


猜数字3

age = 50

count=0

while count < 3:

    _age = int(input("age:"))

    if _age == age :

        print("you guessed right")

        break

    elif _age > age:

         print("The number is too big")

    else:

         print ("The number is too small")

    count +=1

    if count == 3:

        continue_confirm=input("do you want continue.....?")

        if continue_confirm != "n":

            count = 0


0人推荐
随时随地看视频
慕课网APP