day1

第一周 第六章节 第一个python程序

第一个python程序:
#!/usr/bin/env python
#-*- coding:utf-8 _*-
"""
@author:cjs
@file: HelloWorld.py
@time: 2020/05/27
"""
print("Hello World!!!")
print("你好!世界!!!")

D:\Python3.5.2\python.exe "D:/lesson 14/day1/HelloWorld.py"
Hello World!!!
你好!世界!!!

Process finished with exit code 0

 

第一周 第七章节 变量

#!/usr/bin/env python 
#-*- coding:utf-8 _*-
"""
@author:cjs
@file: var1.py
@time: 2020/05/27
"""
name = "chenjisong"
name2 = name
print("my name is ",name,name2)
name = "alex li"
print("my name is ",name,name2)

D:\Python3.5.2\python.exe "D:/lesson 14/day1/var1.py"
my name is chenjisong chenjisong
my name is alex li chenjisong

Process finished with exit code 0

多行注释:

#!/usr/bin/env python 
#-*- coding:utf-8 _*-
"""
@author:cjs
@file: zhushi.py
@time: 2020/05/27
"""
msg = '''
hello 1,
hello 2,
hello 3,
hello 4
'''
print(msg)

D:\Python3.5.2\python.exe "D:/lesson 14/day1/zhushi.py"

hello 1,
hello 2,
hello 3,
hello 4


Process finished with exit code 0

 

第一周 第十章节 用户交互程序

格式化输出第一种: %s占位符

#!/usr/bin/env python 
#-*- coding:utf-8 _*-
"""
@author:cjs
@file: interaction.py 用户交互
@time: 2020/05/27
"""
name = input("name:")
age = input("age:")
job = input("job:")
salary = input("salary:")
info = '''
-------------------info of %s
Name:%s
Age:%s
Job:%s
Salary:%s
''' %(name,name,age,job,salary)
print(info)

D:\Python3.5.2\python.exe "D:/lesson 14/day1/interaction.py"
name:cjs
age:22
job:it
salary:2000

-------------------info of cjs
Name:cjs
Age:22
Job:it
Salary:2000


Process finished with exit code 0

 

强制转换数据类型:

#!/usr/bin/env python 
#-*- coding:utf-8 _*-
"""
@author:cjs
@file: interaction.py 用户交互
@time: 2020/05/27
"""
name = input("name:")
age = int(input("age:"))
print(type(age))
job = input("job:")
salary = float(input("salary:"))
print(type(salary))
info = '''
-------------------info of %s
Name:%s
Age:%d
Job:%s
Salary:%f
''' %(name,name,age,job,salary)
print(info)
%d,%f强制需要在前面加int,float

D:\Python3.5.2\python.exe "D:/lesson 14/day1/interaction.py"
name:cjs
age:23
<class 'int'>
job:it
salary:89
<class 'float'>

-------------------info of cjs
Name:cjs
Age:23
Job:it
Salary:89.000000


Process finished with exit code 0

格式化输出第二种:

#!/usr/bin/env python 
#-*- coding:utf-8 _*-
"""
@author:cjs
@file: interaction.py 用户交互
@time: 2020/05/27
"""
name = input("name:")
age = int(input("age:"))
print(type(age))
job = input("job:")
salary = float(input("salary:"))
print(type(salary))


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

D:\Python3.5.2\python.exe "D:/lesson 14/day1/interaction.py"
name:cjs
age:22
<class 'int'>
job:it
salary:2000
<class 'float'>

-------------------info of cjs
Name:cjs
Age:22
job:it
salary:2000.0


Process finished with exit code 0

格式化输出第三种:

#!/usr/bin/env python 
#-*- coding:utf-8 _*-
"""
@author:cjs
@file: interaction.py 用户交互
@time: 2020/05/27
"""
name = input("name:")
age = int(input("age:"))
print(type(age))
job = input("job:")
salary = float(input("salary:"))
print(type(salary))

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

D:\Python3.5.2\python.exe "D:/lesson 14/day1/interaction.py"
name:cjs
age:22
<class 'int'>
job:it
salary:2000
<class 'float'>

---------------------------info of cjs---------------------------
Name:cjs
Age:22
job:it
salary:2000.0


Process finished with exit code 0

 

第一周 第七章节 if.....else流程判断

单重流程判断:

#!/usr/bin/env python 
#-*- coding:utf-8 _*-
"""
@author:cjs
@file: passwd.py
@time: 2020/05/27
notice:getpass模块在pycharm中不好使
"""
_username="abc"
_passwd="123"
username = input("username:")
passwd = input("passwd:")
#print(username,passwd)
if username == _username and passwd == _passwd:
print("welcome user {name} login".format(name=username))
else:
print("invalid username or password")

多重流程判断:
#!/usr/bin/env python 
#-*- coding:utf-8 _*-
"""
@author:cjs
@file: guess.py 多重判断 if...elif...else
@time: 2020/05/27
"""
age_of_oldboy = 56
guess_age = int(input("guess age:"))

if guess_age == age_of_oldboy:
print("you got it!")
elif guess_age > age_of_oldboy:
print("think smaller!")
else:
print("think bigger!")

第一周 第十二章节 while循环
while循环的第一种写法:
#!/usr/bin/env python
#-*- coding:utf-8 _*-
"""
@author:cjs
@file: guess.py 多重判断 if...elif...else
@time: 2020/05/27
"""
age_of_oldboy = 56

flag = 0
while True:
if flag ==3:
break
guess_age = int(input("guess age:"))
if guess_age == age_of_oldboy:
print("you got it!")
break
elif guess_age > age_of_oldboy:
print("think smaller!")
else:
print("think bigger!")
flag = flag +1

优化版:
#!/usr/bin/env python 
#-*- coding:utf-8 _*-
"""
@author:cjs
@file: guess.py 多重判断 if...elif...else
@time: 2020/05/27
"""
age_of_oldboy = 56
flag = 0
while flag < 3:
guess_age = int(input("guess age:"))
if guess_age == age_of_oldboy:
print("you got it!")
break
elif guess_age > age_of_oldboy:
print("think smaller!")
else:
print("think bigger!")
flag = flag +1
else:
print("you tried too many times....")

D:\Python3.5.2\python.exe "D:/lesson 14/day1/guess.py"
guess age:12
think bigger!
guess age:12
think bigger!
guess age:12
think bigger!
you tried too many times....

Process finished with exit code 0

 

第一周-第十三章节   while循环优化版本

猜数for循环:

#!/usr/bin/env python 
#-*- coding:utf-8 _*-
"""
@author:cjs
@file: guess for.py
@time: 2020/05/27
"""
age_of_oldboy = 56
flag = 0
for i in range(3):
guess_age = int(input("guess age:"))
if guess_age == age_of_oldboy:
print("you got it!")
break
elif guess_age > age_of_oldboy:
print("think smaller!")
else:
print("think bigger!")
flag = flag +1
else:
print("you tried too many times....")

D:\Python3.5.2\python.exe "D:/lesson 14/day1/guess for.py"
guess age:57
think smaller!
guess age:57
think smaller!
guess age:57
think smaller!
you tried too many times....

Process finished with exit code 0


猜数3次,不对的话,计数置零,然后询问是否要继续:
#!/usr/bin/env python 
#-*- coding:utf-8 _*-
"""
@author:cjs
@file: while-if.py
@time: 2020/05/27
"""
age_of_oldboy = 56
count = 0
while count < 3:
guess_age = int(input("guess age:"))
if guess_age == age_of_oldboy:
print("you got it....")
break
elif guess_age > age_of_oldboy:
print("think smaller.....")
else:
print("think bigger......")
count +=1
if count == 3:
continue_confirm = input("do you want to keep guessing.....?")
if continue_confirm != "n":
count = 0

第一周 第十四章节 for循环及作业要求
#!/usr/bin/env python 
#-*- coding:utf-8 _*-
"""
@author:cjs
@file: for.py continue跳出本次循环,进入下一次循环
@time: 2020/05/27
"""
for i in range(10):
if i < 3:
print("loop:",i)
else:
continue
print("hehe...",i)

D:\Python3.5.2\python.exe "D:/lesson 14/day1/for.py"
loop: 0
hehe... 0
loop: 1
hehe... 1
loop: 2
hehe... 2

Process finished with exit code 0



#####小循环与大循环
for i in range(10):
print("-------------------------",i)
for j in range(10):
print("--",j)

D:\Python3.5.2\python.exe "D:/lesson 14/day1/for.py"
------------------------- 0
-- 0
-- 1
-- 2
-- 3
-- 4
-- 5
-- 6
-- 7
-- 8
-- 9
------------------------- 1
-- 0
-- 1
-- 2
-- 3
-- 4
-- 5
-- 6
-- 7
-- 8
-- 9
------------------------- 2
-- 0
-- 1
-- 2
-- 3
-- 4
-- 5
-- 6
-- 7
-- 8
-- 9
------------------------- 3
-- 0
-- 1
-- 2
-- 3
-- 4
-- 5
-- 6
-- 7
-- 8
-- 9
------------------------- 4
-- 0
-- 1
-- 2
-- 3
-- 4
-- 5
-- 6
-- 7
-- 8
-- 9
------------------------- 5
-- 0
-- 1
-- 2
-- 3
-- 4
-- 5
-- 6
-- 7
-- 8
-- 9
------------------------- 6
-- 0
-- 1
-- 2
-- 3
-- 4
-- 5
-- 6
-- 7
-- 8
-- 9
------------------------- 7
-- 0
-- 1
-- 2
-- 3
-- 4
-- 5
-- 6
-- 7
-- 8
-- 9
------------------------- 8
-- 0
-- 1
-- 2
-- 3
-- 4
-- 5
-- 6
-- 7
-- 8
-- 9
------------------------- 9
-- 0
-- 1
-- 2
-- 3
-- 4
-- 5
-- 6
-- 7
-- 8
-- 9

Process finished with exit code 0

 

作业一:博客

作业二:编写登陆接口

  • 输入用户名密码
  • 认证成功后显示欢迎信息
  • 输错三次后锁定

 

作业三:多级菜单
  • 三级菜单
  • 可依次选择进入各子菜单
  • 所需新知识点:列表、字典

posted on 2020-05-27 12:34  orsonchen  阅读(125)  评论(0编辑  收藏  举报

导航