李雄飞
Published on 2017-09-02 11:31 in 暂未分类

python_day1

简介:主要保存简单的条件循环语句,人机交互界面,打印输入语句

1、人机交互(几种字符串拼接)

 1 # Author:Er
 2 # 人机交互界面
 3 """username = input("username: ")
 4 password = input("password: ")
 5 print(username, password)
 6 """
 7 
 8 # 字符串拼接的几种方式 推荐info1 info2
 9 
10 name = 'er'
11 age = '13'
12 info = '''
13 --------- info of '''+name+'''------
14 name : '''+name+'''
15 age : '''+age+'''
16 '''
17 
18 info1 = '''
19 ------info1 of %s ----
20 name : %s
21 age : %s
22 ---------------------
23 ''' % (name, name, age)
24 
25 
26 info2 = '''
27 --------info2 of {_name}----
28 name : {_name}
29 age : {_age}
30 
31 '''.format(_name=name,  _age=age)
32 
33 info3 = '''
34 -----info3 of {0}----
35 name : {0}
36 age : {1}
37 '''.format(name, age)
38 
39 print(info, info1, info2, info3)
40 
41 #input 输入的类型为字符型  <class 'str'>
42 age = input("age: ")
43 print(type(age))
44 
45 # 强制转化 <class 'int'>
46 
47 age = int(input("age :"))
48 print(type(age))
49 
50 # name = raw_input("name: ") python2.x 中写法
Code

 

2、条件与循环语句

 1 # Author:Er
 2 import getpass
 3 _name = 'Er'
 4 _password = 'abc123'
 5 
 6 name = input("name: ")
 7 #password = getpass.getpass("password : ")
 8 #  在pycharm 里不显示,命令行模式下实验
 9 password = input("password: ")
10 
11 if _name == name and _password == password:
12     print("welcome user {name} login ...".format(name=name))
13 else:
14     print("invalid username or password!")
15 while True:
16     break
17 for i in range(10):
18     print("loop: ", i)
19 
20 for j in range(0, 10, 2):      # 0~9,步长为2
21     print("loop1: ", j)
22 
23 k = 0
24 while k < 3:
25     #break  # 非正常退出
26 
27     k = 4  # 正常退出
28 else:
29     print("正常退出时才执行")
30 
31 
32 # print(password)
Code

运行结果

E:\PY\venv\Scripts\python.exe E:/PY/day1/passwd.py
name: er
password: er
invalid username or password!
loop:  0
loop:  1
loop:  2
loop:  3
loop:  4
loop:  5
loop:  6
loop:  7
loop:  8
loop:  9
loop1:  0
loop1:  2
loop1:  4
loop1:  6
loop1:  8

Process finished with exit code 0

  

posted @ 2018-04-15 15:24  李雄飞  阅读(140)  评论(0编辑  收藏  举报