1. 程式人生 > >python基礎1

python基礎1

ant 變量類型 += inf pycha 成功 .get big range

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


python基礎1