简介:主要保存简单的条件循环语句,人机交互界面,打印输入语句
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 : %s21 age : %s22 ---------------------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 输入的类型为字符型42 age = input("age: ")43 print(type(age))44 45 # 强制转化 46 47 age = int(input("age :"))48 print(type(age))49 50 # name = raw_input("name: ") python2.x 中写法
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 break17 for i in range(10):18 print("loop: ", i)19 20 for j in range(0, 10, 2): # 0~9,步长为221 print("loop1: ", j)22 23 k = 024 while k < 3:25 #break # 非正常退出26 27 k = 4 # 正常退出28 else:29 print("正常退出时才执行")30 31 32 # print(password)
运行结果
E:\PY\venv\Scripts\python.exe E:/PY/day1/passwd.pyname: erpassword: erinvalid username or password!loop: 0loop: 1loop: 2loop: 3loop: 4loop: 5loop: 6loop: 7loop: 8loop: 9loop1: 0loop1: 2loop1: 4loop1: 6loop1: 8Process finished with exit code 0