python中小学示范课_python-study-04

上节课复习

上节课复习:

1、数据类型

2、与用户交互

会将用户输入的任何内容都存储字符串类型

python3:(*****)

input

python2(****)

raw_input

python2中还有一个input的,输入什么类型就会存成什么类型,这就要求用户必须输入一个明确的数据类型(**)

3、格式化输出

'my name is %s my age is %s' %('egon',18)

%d:只能接收整型数字

%s:可以接收任意类型

4、运算符

算数运算符

%

/

//

逻辑运算符:and or not

比较运算符

>

<

=

!=

>=

<=

赋值运算符

=

x+=1

x-=1

x*=1

x/=1

x//=1

x**=2

x%=3

链式赋值

a=b=c=d=10

交叉赋值

x=1

y=2

x,y=y,x

变量值的解压

x,*_,y=[1,2,3,4,5,6]

今日内容:

1、流程控制

if判断

while循环

for循环

2、基本数据类型+内置方法

int

float

str

list

元组tuple

dict

set

流程控制之if判断

# 语法1

# if 条件:

# 代码1

# 代码2

# 代码3

# ...

# cls='human'

# sex='female'

# age=18

#

# if cls == 'human' and sex == 'female' and age > 16 and age < 22:

# print('开始表白')

#

# print('end....')

#

#

# 语法2

# if 条件:

# 代码1

# 代码2

# 代码3

# ...

# else:

# 代码1

# 代码2

# 代码3

# ...

# cls='human'

# sex='female'

# age=38

#

# if cls == 'human' and sex == 'female' and age > 16 and age < 22:

# print('开始表白')

# else:

# print('阿姨好')

#

# print('end....')

# 语法3

# if 条件1:

# 代码1

# 代码2

# 代码3

# ...

# elif 条件2:

# 代码1

# 代码2

# 代码3

# ...

# elif 条件3:

# 代码1

# 代码2

# 代码3

# ...

# ............

# else:

# 代码1

# 代码2

# 代码3

# ...

'''

如果:成绩>=90,那么:优秀

如果成绩>=80且<90,那么:良好

如果成绩>=70且<80,那么:普通

其他情况:很差

'''

# score=input('your score: ') #score='73'

# score=int(score) #score=73

# if score >= 90:

# print('优秀')

# elif score >= 80:

# print('良好')

# elif score >= 70:

# print('普通')

# else:

# print('很差')

# user_from_db='egon'

# pwd_from_db='123'

#

# user_from_inp=input('username>>>: ')

# pwd_from_inp=input('password>>>: ')

#

# if user_from_inp == user_from_db and pwd_from_inp == pwd_from_db:

# print('login successfull')

# else:

# print('user or password error')

#if的嵌套

cls='human'

sex='female'

age=18

is_success=False

if cls == 'human' and sex == 'female' and age > 16 and age < 22:

print('开始表白...')

if is_success:

print('在一起')

else:

print('我逗你玩呢....')

else:

print('阿姨好')

print('end....')

流程控制之while循环

#while语法,while循环又称为条件循环

# while 条件:

# code1

# code2

# code3

# ....

# user_db='egon'

# pwd_db='123'

#

# while True:

# inp_user=input('username>>: ')

# inp_pwd=input('password>>: ')

# if inp_user == user_db and inp_pwd == pwd_db:

# print('login successfull')

# else:

# print('user or password error')

#2 while+break:break的意思是终止掉当前层的循环,.执行其他代码

# while True:

# print('1')

# print('2')

# break

# print('3')

# user_db='egon'

# pwd_db='123'

#

# while True:

# inp_user=input('username>>: ')

# inp_pwd=input('password>>: ')

# if inp_user == user_db and inp_pwd == pwd_db:

# print('login successfull')

# break

# else:

# print('user or password error')

# print('其他代码')

#3 while+continue:continue的意思是终止掉本次循环,.直接进入下一次循环

#ps:记住continue一定不要加到循环体最后一步执行的代码

# n=1

# while n <= 10: #

# if n == 8:

# n += 1 #n=9

# continue

# print(n)

# n+=1 #n=11

# while True:

# if 条件1:

# code1

# code2

# code3

# continue #无意义

# elif 条件1:

# code1

# continue #有意义

# code2

# code3

# elif 条件1:

# code1

# code2

# code3

# continue #无意义

# ....

# else:

# code1

# code2

# code3

# continue #无意义

#while循环嵌套

user_db='egon'

pwd_db='123'

while True:

inp_user=input('username>>: ')

inp_pwd=input('password>>: ')

if inp_user == user_db and inp_pwd == pwd_db:

print('login successfull')

while True:

cmd=input('请输入你要执行的命令: ')

if cmd == 'q':

break

print('%s 功能执行...' %cmd)

break

else:

print('user or password error')

print('end....')

#while+tag

user_db='egon'

pwd_db='123'

tag=True

while tag:

inp_user=input('username>>: ')

inp_pwd=input('password>>: ')

if inp_user == user_db and inp_pwd == pwd_db:

print('login successfull')

while tag:

cmd=input('请输入你要执行的命令: ')

if cmd == 'q':

tag=False

else:

print('%s 功能执行...' %cmd)

else:

print('user or password error')

print('end....')

#while+else (***)

n=1

while n < 5:

# if n == 3:

# break

print(n)

n+=1

else:

print('在整个循环结束后,会进行判断:只有while循环在没有被break结束掉的情况下才会执行else中的代码')

流程控制之for循环

# names=['egon','asb','wsb','lsb','csb']

# n=0

# while n < len(names):

# print(names[n])

# n+=1

# names=['egon','asb','wsb','lsb','csb']

# info={'name':'egon','age':18,'sex':'male'}

#

# # for k in info: #x=''age'

# # print(k,info[k])

#

# for item in names:

# print(item)

# for i in range(1,10):

# print(i)

# for i in range(10): #默认的起始位置是0

# print(i)

# for i in range(1,10,2): #1 3 5 7 9

# print(i)

# names=['egon','asb','wsb','lsb','csb']

# for i in range(len(names)):

# print(i,names[i])

# for i in range(5):

# print('========>第一层: %s<=========' %i)

# for j in range(3):

# print(' 第二层: %s' %j)

#for+break

# names=['asb','wsb','egon','lsb','csb']

# for n in names:

# if n == 'egon':

# break

# print(n)

#for+continue

# names=['asb','wsb','egon','lsb','csb']

# for n in names:

# if n == 'egon':

# continue

# print(n)

#for+else

names=['asb','wsb','egon','lsb','csb']

for n in names:

# if n == 'egon':

# break

print(n)

else:

print('=====>')

今日作业:

流程控制作业:

1、编写程序,#根据用户输入内容打印其权限

'''

egon --> 超级管理员

tom --> 普通管理员

jack,rain --> 业务主管

其他 --> 普通用户

'''

user_from_inp=input('username>>:')if user_from_inp == 'egon':print('超级管理员')elif user_from_inp == 'tom':print('普通管理员')elif user_from_inp in ['jack','rain']:print('业务主管')else:print('普通用户')

View Code

2、编写程序,实现如下功能

# 如果:今天是Monday,那么:上班

# 如果:今天是Tuesday,那么:上班

# 如果:今天是Wednesday,那么:上班

# 如果:今天是Thursday,那么:上班

# 如果:今天是Friday,那么:上班

# 如果:今天是Saturday,那么:出去浪

# 如果:今天是Sunday,那么:出去浪

work=['Monday','Tuesday','Wednesday','Thursday','Friday']

rest=['Saturday','Sunday']

today_from_inp=input('today>>:')if today_from_inp inwork:print('上班')elif today_from_inp inrest:print('出去浪')else:print('输入错误')

View Code

3、while循环练习

#1. 使用while循环输出1 2 3 4 5 6 8 9 10

#2. 求1-100的所有数的和

#3. 输出 1-100 内的所有奇数

#4. 输出 1-100 内的所有偶数

#5. 求1-2+3-4+5 ... 99的所有数的和

#6. 用户登陆(三次机会重试)

#7:猜年龄游戏

要求:

允许用户最多尝试3次,3次都没猜对的话,就直接退出,如果猜对了,打印恭喜信息并退出

#8:猜年龄游戏升级版

要求:

允许用户最多尝试3次

每尝试3次后,如果还没猜对,就问用户是否还想继续玩,如果回答Y或y, 就继续让其猜3次,以此往复,如果回答N或n,就退出程序

如何猜对了,就直接退出

#3.1

count=1

while count<=10:print(count)

count+=1

#3.2

count=1sum_100=0while count<=100:

sum_100+=count

count+=1

print(sum_100)#3.3

count=1

while count<=100:if count%2 == 1:print(count)

count+=1

#3.4

count=1

while count<=100:if count%2 ==0:print(count)

count+=1

#3.5

count=1sum_single=0

sum_double=0while count<=99:if count%2 ==0:

sum_double+=countelse:

sum_single+=count

count+=1s=sum_single-sum_doubleprint('sum_single=%s' %sum_single)print('sum_double=%s' %sum_double)print('sum_s-d=%s' %s)#3.6

user_from_db='xjj'pwd_from_db='123'count=1

while count<=3:

user_from_inp=input('username>>:')

pwd_from_inp=input('password>>:')if user_from_db == user_from_inp and pwd_from_db ==pwd_from_inp:print('login success')break

else:print('login failure')

count+=1

#3.7

age_from_db=18count=1

while count<=3:

age_from_inp=int(input('age>>:'))if age_from_db ==age_from_inp:print('bingo')break

else:print('failure')

count+=1

#3.8

age_from_db=18count=1

while count<=3:

age_from_inp=int(input('age>>:'))if age_from_db ==age_from_inp:print('bingo')break

else:print('failure')

count+=1

if count == 4:whileTrue:

cmd= input('retry? Y/N >>:')if cmd == 'Y' or cmd == 'y':

count= 1

break

elif cmd == 'N' or cmd == 'n':break

else:pass

View Code

4、编写计算器程序,要求

1、用户输入quit则退出程序

2、程序运行,让用户选择具体的计算操作是加法or乘法or除法。。。然后输入数字进行运算

3、简单示范如下,可以在这基础上进行改进

while True:

msg='''

1 加法

2 减法

3 乘法

4 除法

'''

print(msg)

choice = input('>>: ').strip()

num1 = input('输入第一个数字:').strip()

num2 = input('输入第二个数字:').strip()

if choice == '1':

res=int(num1)+int(num2)

print('%s+%s=%s' %(num1,num2,res))

tag =Truewhiletag:

msg= '''1 加法

2 减法

3 乘法

4 除法'''

print(msg)whiletag:

choice= input('>>:').strip()if choice == 'quit':

tag=Falseelif choice in ['1','2','3','4']:

num1= input('输入第一个数字:').strip()

num2= input('输入第二个数字:').strip()if choice == '1':

res= int(num1) +int(num2)print('%s+%s=%s' %(num1, num2, res))elif choice == '2':

res= int(num1) -int(num2)print('%s-%s=%s' %(num1, num2, res))elif choice == '3':

res= int(num1) *int(num2)print('%s*%s=%s' %(num1, num2, res))else:

res= int(num1) /int(num2)print('%s/%s=%s' %(num1, num2, res))else:print('请输入指定的值')

View Code

5、基于for循环嵌套实现

5.1 打印九九乘法表

1*1=1

2*1=2 2*2=4

3*1=3 3*2=6 3*3=9

4*1=4 4*2=8 4*3=12 4*4=16

5*1=5 5*2=10 5*3=15 5*4=20 5*5=25

6*1=6 6*2=12 6*3=18 6*4=24 6*5=30 6*6=36

7*1=7 7*2=14 7*3=21 7*4=28 7*5=35 7*6=42 7*7=49

8*1=8 8*2=16 8*3=24 8*4=32 8*5=40 8*6=48 8*7=56 8*8=64

9*1=9 9*2=18 9*3=27 9*4=36 9*5=45 9*6=54 9*7=63 9*8=72 9*9=81

for i in range(1,10):for j in range(1,i+1):print('%s*%s=%s' %(i,j,i*j),end=' ')print()

View Code

5.2 打印金字塔

#max_level=5

* #current_level=1,空格数=4,*号数=1

*** #current_level=2,空格数=3,*号数=3

***** #current_level=3,空格数=2,*号数=5

******* #current_level=4,空格数=1,*号数=7

********* #current_level=5,空格数=0,*号数=9

#数学表达式

空格数=max_level-current_level

*号数=2*current_level-1

max_level=int(input('level>>:'))for current_level in range(1,max_level+1):for space in range(max_level-current_level):print(' ',end='')for asterisk in range(2*current_level-1):print('*',end='')print()

View Code

明天默写:

#1

score=int(input('score>>:'))if score >= 90:print('优秀')elif score >= 80:print('良好')elif score >= 70:print('普通')else:print('很差')

第一题

#2

uname_from_db = 'xjj'pwd_from_db= '123'tag=Truewhiletag:

uname_from_inp= input('username>>:')

pwd_from_inp= input('password>>:')if uname_from_db == uname_from_inp and pwd_from_db ==pwd_from_inp:print('login success')whiletag:

cmd= input('>>:')if notcmd:continue

elif cmd == 'q':

tag=Falseelse:print('%s running...' %cmd)else:print('login failure')

第二题

#3

list = ['egon','asb','bsb','csb','dsb']

dic= {'name':'egon','age':18,'sex':'male'}for i inlist:print(i)for d indic:print(d,':',dic[d])

第三题

明日内容:基本数据类型+内置方法

http://www.cnblogs.com/linhaifeng/articles/7133357.html

学习进程:

http://www.cnblogs.com/linhaifeng/p/7278389.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值