码迷,mamicode.com
首页 > 编程语言 > 详细

python基础

时间:2016-07-09 00:33:26      阅读:223      评论:0      收藏:0      [点我收藏+]

标签:

导入getpass掩码模块:
1
import getpass 2 user = raw_input("pls user:") 3 passwd = getpass.getpass("pls passwd:") 4 print(user,passwd)
输出的结果展示为:
  技术分享
 1 #每一行最多不能超过80字符
 2 user_input = input("input your name:")
 3 age_input = input("input your age:")
 4 work_input = input("input your work:")
 5 msg=‘‘‘
 6 Infomation of %s:
 7 -----------------
 8 Name : %s
 9 Age  : %s
10 Work : %s
11 -------End-------
12 ‘‘‘ %(user_input,user_input,age_input,work_input)
13 print(msg)
1 #在linux自己写的模块放在这个目录下:/usr/lib/python2.7/dist-packages,import便可直接导入
2 import sys
3 print(sys.path)#打印系统的环境变量
1 import os
2 os.system("dir f:\Java\code")#查看这个目录下的内容
3 
4 os.mkdir("han")#创建han文件夹
5 
6 cmd = os.popen("dir f:\Java\code").read()
7 print(cmd)  #将要查看的内容保存在一个变量里然后打印出来
 1 if语句 v1
 2 user = "han"
 3 passwd = "han"
 4 
 5 username = input("username:")
 6 password = input("password:")
 7 
 8 if user == username:
 9     print("user the same is username!")
10     if password == passwd:
11         print("welcome login!")
12     else:
13         print("password is invalid..")
14 else:
15     print("you get out!")
 1 if语句 v2
 2 user = "han"
 3 passwd = "kai"
 4 
 5 username = input("username:")
 6 password = input("password:")
 7 
 8 if user == username and passwd == password:
 9     print("welcome lgin!")
10 else:
11     print("invalid username or password..try again!")

循环语句:

 猜年龄:
1
age = 26 2 3 guess_num = int(input("input your guess num:")) 4 5 if guess_num == age: 6 print("Congratulations! you got it.") 7 elif guess_num > age: 8 print("think smaller!") 9 else: 10 print("think big!")
 只能猜三次年龄:
1
age = 26 2 3 for i in range(10): 4 if i < 3: 5 guess_num = int(input("input your guess num:")) 6 7 if guess_num == age: 8 print("Congratulations! you got it.") 9 break 10 elif guess_num > age: 11 print("think smaller!") 12 else: 13 print("think big!") 14 else: 15 print("Too many attempts..bye ") 16 break
 最终优化版
1
age = 25 2 3 counter = 0 4 for i in range(10): 5 if counter < 3: 6 guess_num = int(input("pls input an number:")) 7 if guess_num == age: 8 print("you are great!") 9 break 10 elif guess_num > age: 11 print("the bigger") 12 else: 13 print("the smaller") 14 else: 15 continue_comfirm = input("do you want to continue?") 16 if continue_comfirm == "y": 17 counter = 0 18 continue 19 else: 20 print("bye") 21 break 22 counter += 1

 

python基础

标签:

原文地址:http://www.cnblogs.com/hanxiaobei/p/5654877.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!