L001-老男孩教育-Python13期VIP视频-19节-pbb
L001-老男孩教育-Python13期VIP视频-19节-pbb
Windows上安装 Python3开发环境
下载:www.python.org >选择Downloads>All releases- python3.5.2
设置环境变量:编辑>系统变量Path>在最后加入 ;D:\software\Python35
这里要把python.exe重命名为python3.exe
#推荐在脚本里这样写命令
#下面写法也可以,但是不推荐。
更换python解释版本
#打印用户输入
在pycharm里
ctrl+d 复制一行
#格式化字符串
name = input("input your name:")
age = int(input("input your age:")) #将接受到的字符串强制转换成数字
job = input("input your job:")
print("name is:",name)
print("age is:",age)
print("job is:",job)
msg = '''
Information of user %s:
--------------------
name: %s
age: %d
job: %s
-------End----------
'''%(name,name,age,job) #%s字符串占位符,%d数字整数占位符,%f数字小数占位符。
print(msg)
#不明文显示密码。这一段在pycharm里不好用,但是可以在Linux里正常执行。
#Linux下执行
import os
os.system('df')
os.mkdir('yourDir')
cmd_res = os.popen("df -h").read() #将df -h命令的输出保存到cmd_res里
#观察python的全局变量
>>> import sys
>>> print(sys.path)
['', '/usr/local/lib/python27.zip', '/usr/local/lib/python2.7', '/usr/local/lib/python2.7/plat-linux2', '/usr/local/lib/python2.7/lib-tk', '/usr/local/lib/python2.7/lib-old', '/usr/local/lib/python2.7/lib-dynload', '/usr/local/lib/python2.7/site-packages']
>>>
[root@bolin ~]# cd /usr/local/lib/python2.7/site-packages
[root@bolin site-packages]# ls #将常用导入的包就放到这个目录里
READMEtab.py
#登录密码v1
user = 'alex'
passwd = 'alex3714'
username = input("username:")
password = input("pawwsord:")
if user == username:
print("username is correct...")
if password
== passwd:
print("Welcome login...")
else:
print("password is invalid...")
else:
print("连用户名都没蒙对!滚出!!!")
#登录密码v2
user = 'alex'
passwd = 'alex3714'
username = input("username:")
password = input("pawwsord:")
if user == username and passwd
== password:
print("Welcome login")
else:
print("Invalid username or password.")
pycharm里
缩进:选中 - tab
往回缩进:选中 - shift+tab
#猜年龄游戏
age = 22
counter = 0
for i in range(10):
print('------------->counter:',counter)
if counter < 3 :
guess_num = int(input("input your guess num:"))
if guess_num == age:
print("Congratulations!you got it.")
break #不往后走了,跳出整个loop。
elif guess_num > age:
print("Think smaller!")
else:
print("Think Big...")
else:
#print("too many attempts...bye")
#break
continue_confirm = input("Do you want to continue because you are stupid?")
if continue_confirm == 'y' :
counter = 0
continue
else:
print("bye")
break
counter += 1