您的位置:首页 > 编程语言 > Python开发

python学习之路(第一天)

2018-03-26 11:24 204 查看

研究僧在读python小白,有些许c语言基础,在此附上自己学习python之路,除了部分精要之处详解,其余部分楼主只附上代码,留作回顾!

字符串要点

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:")
#第一种,%s占位,% (name,name,age,job,salary)对应补位
info = '''
-------- info of  %s  ------
Name:%s
Age:%d
Job:%s
Salary:%s
''' % (name,name,age,job,salary)

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

#类似2
info3 =  '''
-------- info of {0} -----
Name:{0}
Age:{1}
Job:{2}
Salary:{3}
'''.format(name,age,job,salary)
print(info)
#print(info2)
#print(info3)

循环要点(while, break,continue, if ,elif, for)

age = 56
count = 0
while count <3:#运行三次
guess_age = int(input("guess age:") )
 if guess_age == age :
print("yes, you got it. ")
break  #猜对了就跳出循环
elif guess_age > age: #c语言是 else if
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':#输入n结束程序,否则继续执行
count =0
age = 56
child=10
count=0
while count<2:
for i in range(3):  #加入for
guess_age = int(input("guess age:") )
if guess_age == age :
print("yes, you got it. ")
break
elif guess_age > age:
print("think smaller...")
elif guess_age< child:
print("i'm not a chlid, you idiot!")
else:
continue
count+=1;
if count==2:
guess=input("You serious? give it up!")
if guess !='ok':
count=0
else:
print("you have tried too many times..fuck off")
判断用户
import getpass
_username = 'shiwei'
_password = 'abc123'
username = input("username:")
password = getpass.getpass(" Input the password:") 
#password = input("password:")
#if _password == password and _username=username:
if _password == password:
    print("Welcome user {name} login...".format(name=username))
else:
    print("Invalid username or password!")

#pycharm 不支持getpass, 可以将第5句和第8句换成 6-7句来判断。
未完待续。。。

阅读更多
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: