Python学习第一课

#变量
    age=18
    #定义一个变量,会有三个特征:id(内存地址),type(类型),value(地址)
    print(id(age),type(age),age)

    单行注释(#)的快捷键 Windows下为 ctrl+?    Mac下为 command+?
    多行注释为一对三个双引号(""" """#coding = utf-8 设置字符集
    #变量的命名方式
    # 1.驼峰体
    # AgeOldBoy = 73  首字母大写
    # 2.下划线
    # age_of_oldboy = 84

#常量
      不可变化的量

用户与程序交互
在Python3中input()无论用户输入的是什么,都解释成为字符串
name = input('请输入你的用户名: ')
print(id(name),type(name),name) #name = 18
在Python2中input()用户必须输入值,输入的是什么类型 就存成什么类型。raw_input()与Python3中的input()使用一样。
浮点型float
heght = 1.81
print(type(heght),heght)
#整型 int
age = 18
print(type(age),age)
#字符串 str
name = '老男孩'
print(type(name),name)

#字符串拼接
name = 'egon'
msg = 'hello'
print(name + msg)
name = '字符串'
print(name*10)

列表:可以存储多个值。定义在中括号内可以用逗号分开的多个元素。元素可以是任意类型。
hobbies = ['play','read','music','movie']
print(hobbies[3])
print(hobbies[-1])
列表嵌套

字典
hobbies = {'name':'sipeng','age':18,'heght':1.89}
print(hobbies['name'])
print(hobbies['age'])
print(hobbies['heght'])
字典嵌套:字典嵌套字典,字典嵌套列表

布尔类型
# ture
# false
age =19
AGE =20
print(age>AGE)
print(age<AGE)


格式化输出
占位符 %s   %d
%s 可以接收整型和字符串类型
%d 只可以接收整型
my name is xxx,my age is xxx
name = input('user_name: ')
age = input('user_age: ')
print('my name is %s,my age is %s' %(name,age))
print('my name is %s,my age is %d' %(name,int(age)))

逻辑运算符
andornot
and  两边都成立才为Ture 否则为False
or   两边又一边成立就为Ture 否则为False
not  取反


流程控制语句 if...else...
age = input('>>>: ')
age = int(age)
if age > 30:
    print('叫阿姨~')
else:
    print('叫妹妹~')

age = int(input('>>>: '))
sex = input('>>>: ')
is_pretty = bool(input('>>>: '))
if sex == 'female' and age < 30 and is_pretty == True:
    print('表白中~')
else:
    print('叫阿姨~')

while循环
while 条件:
    循环体

count = 0
while count < 3:
    print('lopo',count)
    count += 1

死循环
while True:
    print('死循环!')


跳出while循环 brerk
count = 0
while True:
    if count > 100:
        break
    print(count)
    count += 1

continue 跳出本次循环
count = 0
while True:
    if count <= 10:
        if count == 7:
            count += 1
            continue
        print(count)
        count += 1
课后作业:
1.
# 基础需求:
#     让用户输入用户名密码
#     认证成功后显示欢迎信息
#     输错三次后退出程序
user_name = 'pengsilong'
pass_word = '111111'

count = 0
while count<3:
    username = input("please input your username: ")
    password = input("please input your password: ")
    if username == user_name and password == pass_word:
        print("welcome to login! " + username)
        break
    else:
        print("your username or password is error!")
        count += 1

2.
# 基础需求:
#     让用户输入用户名密码
#     认证成功后显示欢迎信息
#     输错三次后退出程序
# 升级需求:
#     可以支持多个用户登录 (提示,通过列表存多个账户信息)
#     用户3次认证失败后,退出程序,再次启动程序尝试登录时,还是锁定状态(提示:需把用户锁定的状态存到文件里)

name = ["张三","李四","王五"]
pwd = ["111111","222222","333333"]
count = 0

while True:
    username = input("请输入你的用户名: ")
    if username not in name:
        print("你输入的用户不存在~")
        continue
    else:
        with open("lock_name.txt","r") as usr:
            king = usr.read()
            if username in king:
                print(username + " 该用户已经被锁定~")
                break
    userpass = input("请输入你的密码: ")

    if username == name[0] and userpass == pwd[0]:
        print("登录成功~" + username)
        break
    elif username == name[1] and userpass == pwd[1]:
        print("登录成功~" + username)
        break
    elif username == name[2] and userpass == pwd[2]:
        print("登录成功~" + username)
        break
    else:
        count += 1
        print("用户名或密码错误~")
        if count >= 3:
            with open("lock_name.txt","a") as w:
                w.write(username + ",")
                print("输入密码错误次数过多 %s 已经被锁定"%username)
                break
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值