1.if函数
(1)定义
if 要判断的条件:
条件成立时,要做的事
elif 要判断的条件:
条件成立时,要做的事
else :
条件都不成立时要做的事
age = 18
if age >= 18:
print('你已经成年,可以进入本网吧')
else:
print('未满18岁,不可进入')
(2)逻辑运算符
and
条件1 and 条件2
两个条件同时满足,就返回True
两个条件有一个不满足,就返回False
age = 19
if age >= 0 and age <= 18:
print('未成年')
else:
print('成年')
or:
条件1 or 条件2
两个条件只要有一个满足,就返回True
两个条件都不满足,返回False
if嵌套
if 要判断的条件:
条件成立时,要做的事情
elif 条件2:
…
else:
条件不成立时,要做的时请
注意:elif和else都必须和if联合使用,不能单独使用
holiday_name = '植树节'
if holiday_name == '情人节':
print('买礼物,买花,买包,买化妆品')
elif holiday_name == '愚人节':
print('吃饭')
else:
print('不过节')
have_ticket = True
knife_length = 21
if have_ticket:
print('已有车票,请安检...')
if knife_length > 20:
print('长度为 %d:超出限定长度,禁止入内' %knife_length)
else:
print('长度为 %d:没有超过限定长度,允许入内' %knife_length)
else:
print('请先买票')
判断闰年?
用户输入年份year, 判断是否为闰年?
能被4整除但不能被100整除的 或者 能被400整除 那么就是闰年
year = int(input('输入年份:'))
sc = year%4
yc = year%100
sy = year%400
print(sy)
if sc == 0 and yc == 0:
print('不是闰年')
elif sc == 0 and yc !=0:
print('是闰年')
elif sy == 0:
print('是闰年')
else:
print('不是闰年')
输入年、月,输出本月有多少天。合理选择分支语句完成设计任务。
输入样例1:2004 2
输出结果1:本月29天
输入样例2:2010 4
输出结果2:本月30天
year = int(input(‘输入年份:’))
month = int(input(‘输入月份:’))
sc = year%4
yc = year%100
sy = year%400
if sc == 0 and yc == 0:
rn=0
elif sc == 0 and yc != 0:
rn=1
else:
rn=0
if month== 1 or month== 3 or month== 5 or month== 7 or month== 8 or month== 10 or month== 12:
print('本月有31天')
elif month== 4 or month== 6 or month== 9 or month== 11 :
print('本月有30天')
elif month==2:
if rn == 1 :
print('本月有29天')
elif rn == 0 :
print('本月有28天')
猜拳游戏:
从控制台输入要出的拳 --石头1 剪刀2 布3
电脑随机出拳
比较胜负
(1)
import random
#1.从控制台输入要出的拳 --石头1 剪刀2 布3
player = int(input("请输入您要出的拳: 石头1/剪刀2/布3 :"))
#2.电脑随机出拳
computer = random.randint(1,3)
print(computer)
#3.比较胜负
if ((player == 1 and computer == 2)
or (player == 2 and computer == 3)
or (player == 3 and computer == 1)):
print('玩家胜利~')
elif player == computer:
print('平局')
else:
print('玩家失败~')
(2)
import random
date=(input('出拳:'))
if date=='石头':
a=1
elif date=='剪刀':
a=2
elif date=='布':
a=3
b=(random.randint(1,3))
if a== 1 and b== 1:
print('平局')
elif a== 1 and b== 2:
print('玩家获胜')
elif a== 1 and b== 3:
print('电脑获胜')
if a== 2 and b== 2:
print('平局')
elif a== 2 and b== 3:
print('玩家获胜')
elif a== 2 and b==1:
print('电脑获胜')
if a== 3 and b== 3:
print('平局')
elif a== 3 and b== 1:
print('玩家获胜')
elif a== 3 and b== 2:
print('电脑获胜')
![在这里插入图片描述](https://img-blog.csdnimg.cn/20190322103733260.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3F3cXEyMzM=,size_16,color_FFFFFF,t_70)
2.for循环
(1)for循环使用的语法:
for 变量 in 序列:
循环要执行的动作
c语言中用for(i = 1; i <= 100 ; i++)控制循环
在python中,则需引用range变量
range(stop): 0 stop-1
range(start,stop): start stop-1
range(start,stop,step): start stop-1 step(步长)
输出1,2,3,4,5中不重复的三位数的个数(115这类也不算)
count=0
for x in range(1,5):
for y in range(1,5):
for z in range(1,5):
if x!=y and y!=z and x!=z:
print(x*100+y*10+z)
count +=1
print (count)
设置指定的用户名和密码,用户只可登陆三次,若都不正确则退出
(1)while控制
i=3
while i>0:
name = input('用户名:')
password = input ('密码:')
if name=='root' and password=='westos' :
print('登陆成功')
break
else :
print('登录失败')
print('还剩余%d次机会'%(i-1))
i = i-1
else:
print ('登陆机会超过三次')
(2)for控制
for i in range(3):
name = input('用户名: ')
password = input('密码: ')
if name == 'root' and password == 'westos':
print('登录成功')
break
else:
print('登录失败')
print('您还剩余%d次机会' %(2-trycount))
trycount += 1
else:
print('登录次数超过3次,请稍后再试!')
用户输入一个整型数,求该数的阶乘
num=int(input(‘输入你想要求阶乘的数:’))
result=1
for i in range(1,num+1):
result *= i
print(’%d的阶乘是%d’%(num,result))
中断变量
break:跳出整个循环,不会再循环后面的内容
continue:跳出本次循环,continue后面的代码不再执行,但是循环依然继续
exit():结束程序的运行
for i in range(10):
if i == 5:
# break
continue
print('hello python')
# exit()
print(i)
print(‘hello world’)
3.while循环
“”"
while 条件():
条件满足时,做的事情1
条件满足时,做的事情2
"""
i = 1 #定义一个变量,记录循环次数
while i <= 3: #开始循环
print('hello python') #循环内执行的动作
i += 1 #处理计数器
while死循环
while True:
print('hello python')
while求和
i = 0
sum = 0
while i<=100 :
sum += i
i += 1
print(sum)
测试:
在控制台连续输出五行*,每行依次递增
(1)
*
**
***
****
*****
row = 5
while row >= 1:
col = 5
while col >= row:
print('*',end='')
col -= 1
print('')
row -= 1
(2)
*****
****
***
**
*
row = 5
while row >= 1:
col = 1
while col <= row:
print('*',end='')
col += 1
print('')
row -= 1
(3)
*
**
***
****
*****
n = 1
while n <= 5:
l = 1
while l <= 5 - n:
print(' ', end='')
l += 1
j = 1
while j <= n and j <= 5:
print('*', end='')
j += 1
print()
n += 1
(4)
*****
****
***
**
*
hang = 0
while hang <= 4:
lie = 1
while lie <= hang:
print(' ', end='')
lie += 1
while lie >= hang and lie <= 5:
print('*', end='')
lie += 1
print()
hang += 1
猜数字游戏
1. 系统随机生成一个1~100的数字;
2. 用户总共有5次猜数字的机会;
3. 如果用户猜测的数字大于系统给出的数字,打印“too big”;
4. 如果用户猜测的数字小于系统给出的数字,打印"too small";
5. 如果用户猜测的数字等于系统给出的数字,打印"恭喜",并且退出循环;
import random
trycount = 0
computer = random.randint(1,100)
print(computer)
while trycount < 5:
player = int(input('Num:'))
if player > computer:
print('too big')
trycount += 1
elif player < computer:
print('too small')
trycount += 1
else:
print('恭喜')
break