week1:python if_for_while 基本用法

w1_if_for_while:仅为自己学习记录之用

5.Python3.5-开课介绍2

擅长领域

7、第一周-第07章节-Python3.5-变量

pycharm设置

变量定义规则:

1.变量只能是字母、数据或下划线的任意组合
2.变量名的第一个字符不能是数字
3.以下关键字不能声明为变量名

and, as, assert, break, class, continue, def, del, elif, else, except, exec, finally, for, from, global, if, import, in, is, lambda, not, or, pass, print, raise, return, try, while, with, yield

9、第一周-第09章节-Python3.5-字符编码的区别与介绍

10、第一周-第10章节-Python3.5-用户交互程序

关于注释

单行注释:#code
多行注释:‘’’ code '''

多行输出

打印多行:
msg='''
line 1
line 2
line 3
...

line n
'''
print(msg)

占位格式输出

name = input("name:")
age = input("age:")
job = input("job:")

info='''
------info %s-----
name:%s
age:%d
job:%s
'''% (name,name,age,job)
print(info)
%s 代表 string
%d 代表 digital

19:57

拼接格式输出

info='''
------info '''+  name + '''-----
name:''' + name +'''
age:''' + age + '''
job:''' + job
print(info)

变量赋值格式输出

info='''
------info {NAME}-----
name:{NAME}
age:{AGE}
job:{Job}
'''.format (NAME=name,AGE=age,Job=job)

print(info)

11、第一周-第11章节-Python3.5-if else流程判断

密码以密文显示

import getpass
password = getpass.getpass("password:")
print(password)

用户名密码判断

_username="testuser"
_password="p123"
username = input("username:")
password = input("password:")
if username == _username and password == _password :
    print("welcome user {user_name}".format(user_name=username))
else:
    print("invalid password or username")

12、第一周-第12章节-Python3.5-while 循环

13、第一周-第13章节-Python3.5-while 循环优化版本

猜年龄

real_age = 18
count = 0
while count < 3:
    input_age = int(input("age:"))
    if input_age > real_age :
        print("you think bigger than real age!")
    elif input_age == real_age:
        print("yes,you got it")
        break
    else:
        print("you think smaller than real age!")
    count = count + 1
else:
    print("you have tried too many times!")

for循环

for i in range(0,10)
    print(i)

for循环,使用步长

for i in range(0,10,3)
    print(i)
```10:16
```

while循环3次后询问是否继续

real_age = 18
count = 0
while count < 3:
    input_age = int(input("age:"))
    if input_age > real_age :
        print("you think bigger than real age!")
    elif input_age == real_age:
        print("yes,you got it")
        break
    else:
        print("you think smaller than real age!")
    count += 1
    if count == 3:
        continue_confirm = input("do you need to continue?")
        if continue_confirm == "n":
            break
        else:
            count = 0

14、第一周-第14章节-Python3.5-for 循环及作业要求

continue 跳出本次循环,继续下一次循环

for i in range(0,10):
    if i < 5:
        print("loop",i)
    else:
        continue
    print("hehe...")

day1-homework

写博客

编写登陆接口

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

多级菜单

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

作业要求

1,博客

2.流程图

3.README

4.code

day1完成作品

登陆接口流程图

 

README

python environment required:
python 3.5
how to start program:
python login_code.py

login_code.py

import getpass

with open('user_password.txt','r') as f:
    dict = eval(f.read())
f.close()
print(list(dict.keys()))
username = input("username:")
count = 0
while count < 3:
    # password = input("password:")
    password = getpass.getpass("password:")
    if username in list(dict.keys()):
        is_lock = dict[username][1]
        if is_lock == "true":
            print(username + " is locked")
            break
        elif password == dict[username][0]:
            print("welcome " + username)
            break
        elif password != dict[username][0]:
            print("Invalid username or password")
            count = count + 1
            if count == 3:
                dict[username][1] = 'true'
                with open("user_password.txt", 'w') as f:
                    f.write(str(dict))
                f.close()
    else:
        print(username + " not exist!")
        break
else:
    print("you tried too many times!")

猜你喜欢

转载自www.cnblogs.com/rootid/p/9315542.html